<?php
namespace App\Api\Controllers\User;


use Aliyun\AliSms;
use Alipayopen\Sdk\AopClient;
use Alipayopen\Sdk\Request\AntMerchantExpandOrderQueryRequest;
use App\Api\Controllers\BaseController;
use App\Api\Controllers\Basequery\StorePayWaysController;
use App\Api\Controllers\Config\AlipayIsvConfigController;
use App\Api\Controllers\Config\CmbcPayConfigController;
use App\Api\Controllers\Config\EasyPayConfigController;
use App\Api\Controllers\Config\FuiouConfigController;
use App\Api\Controllers\Config\HkrtConfigController;
use App\Api\Controllers\Config\HwcPayConfigController;
use App\Api\Controllers\Config\JdConfigController;
use App\Api\Controllers\Config\LinkageConfigController;
use App\Api\Controllers\Config\NewLandConfigController;
use App\Api\Controllers\Config\TfConfigController;
use App\Api\Controllers\Config\VbillConfigController;
use App\Api\Controllers\Config\WeixinConfigController;
use App\Api\Controllers\Config\WftPayConfigController;
use App\Api\Controllers\Hltx\ManageController;
use App\Api\Controllers\MyBank\MyBankController;
use App\Api\Controllers\Newland\UpdateController;
use App\Api\Controllers\Vbill\PayController;
use App\Api\Controllers\Weixin\PayController as WeixinPayController;
use App\Models\AlipayAppOauthUsers;
use App\Models\AlipayZftStore;
use App\Models\ChangshaStore;
use App\Models\CcbStores;
use App\Models\DadaMerchant;
use App\Models\DadaStore;
use App\Models\Device;
use App\Models\EasypayaStore;
use App\Models\ExtensionWorkers;
use App\Models\ExtensionWorkerStores;
use App\Models\NongjinStores;
use App\Models\PinganStores;
use App\Models\GsNongjinStores;
use App\Models\AbcStores;
use App\Models\AbcquietStores;
use App\Models\AllinpayStores;
use App\Models\MerchStores;
use App\Models\WftpayStoreV2;
use App\Models\WingStores;
use App\Models\SaobeiStores;
use App\Models\CoolStores;
use App\Models\DlbStore;
use App\Models\EasypayStore;
use App\Models\FuiouStore;
use App\Models\HltxStore;
use App\Models\HkrtBusinessScope;
use App\Models\HkrtStore;
use App\Models\HStore;
use App\Models\HwcpayStore;
use App\Models\JdStore;
use App\Models\LianfuStores;
use App\Models\LianfuyoupayStores;
use App\Models\LinkageStores;
use App\Models\LtfStore;
use App\Models\Merchant;
use App\Models\MerchantStore;
use App\Models\MyBankStore;
use App\Models\NewLandConfig;
use App\Models\NewLandStore;
use App\Models\Order;
use App\Models\ProvinceCity;
use App\Models\SmsConfig;
use App\Models\Store;
use App\Models\StoreBank;
use App\Models\StoreImg;
use App\Models\StorePayWay;
use App\Models\SuzhouStore;
use App\Models\TfConfig;
use App\Models\TfStore;
use App\Models\User;
use App\Models\UserRate;
use App\Models\UserStoreSet;
use App\Models\VbillaStore;
use App\Models\VbillStore;
use App\Models\VoiceStatistic;
use App\Models\WeixinaStore;
use App\Models\WeixinStore;
use App\Models\WftpayStore;
use App\Models\YinkaStore;
use App\Services\Count\OrderService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Cache;
use Rtgm\sm\RtSm2;
class StoreController extends BaseController
{
    public function up_store_quota (Request $request)
    {
        try {
            $user = $this->parseToken();
            if ($user->pid != 0) {

                if (!in_array($user->phone, [19956887919])) {
                    $this->status = -1;
                    $this->message = '暂无权限，请联系服务商进行操作';
                    return $this->format();
                }

            }






            $storeId = $request->input('store_id');
            $isQuota = $request->input('is_quota');
            if ($storeId) {
                $pass = $request->get('pass', '');
                $pass = substr(base64_decode($pass), 0, -5);
                $pass_id = DB::table('pass')->where(['pass' => $pass])->value('id'); // 密钥id
                $remark = '更改额度开关 ' . $storeId .'--'. $isQuota;
                $this->operation_log( $this->parseToken()->user_id, 28, $remark , $pass_id );

                $rs = Store::where('store_id', $storeId)->update(['is_quota' => $isQuota]);
                if ($rs) {
                    $this->status = 1;
                    $this->message = '更新成功';
                    return $this->format();
                } else {
                    $this->status = -1;
                    $this->message = '更新失败';
                    return $this->format();
                }
            }
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }

    public function up_store_refund (Request $request)
    {
        try {
            $user = $this->parseToken();
            if ($user->pid != 0) {

                if (!in_array($user->phone, [19956887919])) {
                    $this->status = -1;
                    $this->message = '暂无权限，请联系服务商进行操作';
                    return $this->format();
                }

            }
            $storeId = $request->input('store_id');
            $isBeforeRefund = $request->input('is_before_refund');
            if ($storeId) {
                $rs = Store::where('store_id', $storeId)->update(['is_before_refund' => $isBeforeRefund]);
                if ($rs) {
                    $this->status = 1;
                    $this->message = '更新成功';
                    return $this->format();
                } else {
                    $this->status = -1;
                    $this->message = '更新失败';
                    return $this->format();
                }
            }
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }
    public function up_store_goods (Request $request)
    {
        try {
            $user = $this->parseToken();
            if ($user->pid != 0) {

                if (!in_array($user->phone, [19956887919])) {
                    $this->status = -1;
                    $this->message = '暂无权限，请联系服务商进行操作';
                    return $this->format();
                }

            }
            $storeId = $request->input('store_id');
            $isGoods = $request->input('is_goods');
            $goodsCost = $request->input('goods_cost');
            $agentCost = $request->input('agent_cost');
            if ($storeId) {

                $btn1 = $request->input('btn1', 1);
                $btn2 = $request->input('btn2', 1);
                if (DB::table('store_goods_btn')->where('store_id', $storeId)->first()) {
                    DB::table('store_goods_btn')->where('store_id', $storeId)->update(['btn1' => $btn1, 'btn2' => $btn2]);
                } else {
                    DB::table('store_goods_btn')->insert(['store_id' => $storeId, 'btn1' => $btn1, 'btn2' => $btn2]);
                }

                $rs = Store::where('store_id', $storeId)->update(['is_goods' => $isGoods, 'goods_cost' => $goodsCost, 'agent_cost' => $agentCost]);
                if ($rs) {
                    $this->status = 1;
                    $this->message = '更新成功';
                    return $this->format();
                } else {
                    $this->status = -1;
                    $this->message = '更新失败';
                    return $this->format();
                }
            }
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }

    /**
     * 门店列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store_lists(Request $request)
    {
        try {
            $user = $this->parseToken();
            $user_id = $request->get('user_id', $user->user_id);
            $time_start = $request->get('time_start', '');//关键词
            $time_end = $request->get('time_end', '');//关键词
            $store_name = $request->get('store_name', '');//关键词
            $province_code = $request->get('province_code');//状态
            $city_code = $request->get('city_code');//状态
            $area_code = $request->get('area_code');//状态
            $status = $request->get('status');//状态
            $pid = $request->get('pid', 0);//主店ID
            $is_delete = $request->get('is_delete', 0);//
            $is_close = $request->get('is_close', 0);//

            $where = [];

            $where[] = ['stores.pid', '=', $pid];
            $where[] = ['stores.is_delete', '=', $is_delete];
            if($is_close!==""){
                $where[] = ['stores.is_close', '=', $is_close];
            }
            if ($status) {
                $where[] = ['stores.status', '=', $status];
            }
            if ($province_code) {
                $where[] = ['stores.province_code', '=', $province_code];
            }
            if ($city_code) {
                $where[] = ['stores.city_code', '=', $city_code];
            }
            if ($area_code) {
                $where[] = ['stores.area_code', '=', $area_code];
            }
            if ($time_start) {
                $time_start = date('Y-m-d 00:00:00', strtotime($time_start));
                $where[] = ['stores.created_at', '>=', $time_start];
            }
            if ($time_end) {
                $time_end = date('Y-m-d 23:59:59', strtotime($time_end));
                $where[] = ['stores.created_at', '<=', $time_end];
            }

            if (env('DB_D1_HOST')) {
                $obj = DB::connection("mysql_d1")->table("stores");
            } else {
                $obj = DB::table('stores');
            }
            $obj = $obj->join('users', 'stores.user_id', 'users.id');
            if($user_id==101){
                $where[] = ['stores.ascription', '=', 1];
                if ($store_name) {
                    if (is_numeric($store_name)) {
                        $where1[] = ['stores.store_id', 'like', '%' . $store_name . '%'];
                    } else {
                        $where1[] = ['stores.store_name', 'like', '%' . $store_name . '%'];
                    }
                    $obj->where($where1)
                        ->whereIn('stores.user_id', $this->getSubIds(1))
                        ->select('stores.*', 'users.name as user_name')
                        ->orderBy('stores.updated_at', 'desc')
                        ->get();

                } else {
                    $where[] = ['stores.ascription', '=', 1];
                    $obj->where($where)
                        ->whereIn('stores.user_id', $this->getSubIds(1))
                        ->select('stores.*', 'users.name as user_name')
                        ->orderBy('stores.updated_at', 'desc')
                        ->get();
                }
            }else if($user_id==747){
                $where[] = ['stores.ascription', '=', 4];
                if ($store_name) {
                    if (is_numeric($store_name)) {
                        $where1[] = ['stores.store_id', 'like', '%' . $store_name . '%'];
                    } else {
                        $where1[] = ['stores.store_name', 'like', '%' . $store_name . '%'];
                    }
                    $obj->where($where1)
                        ->whereIn('stores.user_id', $this->getSubIds(1))
                        ->select('stores.*', 'users.name as user_name')
                        ->orderBy('stores.updated_at', 'desc')
                        ->get();

                } else {
                    $where[] = ['stores.ascription', '=', 4];
                    $obj->where($where)
                        ->whereIn('stores.user_id', $this->getSubIds(1))
                        ->select('stores.*', 'users.name as user_name')
                        ->orderBy('stores.updated_at', 'desc')
                        ->get();
                }
            }else if($user_id == 1002){
                $where[] = ['stores.ascription', '=', 6];
                if ($store_name) {
                    if (is_numeric($store_name)) {
                        $where1[] = ['stores.store_id', 'like', '%' . $store_name . '%'];
                    } else {
                        $where1[] = ['stores.store_name', 'like', '%' . $store_name . '%'];
                    }
                    $obj->where($where1)
                        ->whereIn('stores.user_id', $this->getSubIds(1))
                        ->select('stores.*', 'users.name as user_name')
                        ->orderBy('stores.updated_at', 'desc')
                        ->get();

                } else {
                    $where[] = ['stores.ascription', '=', 6];
                    $obj->where($where)
                        ->whereIn('stores.user_id', $this->getSubIds(1))
                        ->select('stores.*', 'users.name as user_name')
                        ->orderBy('stores.updated_at', 'desc')
                        ->get();
                }
            }else if($user_id == 347){
                $obj = DB::table('pingan_stores')->join('stores', 'pingan_stores.store_id', 'stores.store_id')->join('users', 'stores.user_id', 'users.id');
                if ($store_name) {
                    if (is_numeric($store_name)) {
                        $where1[] = ['stores.store_id', 'like', '%' . $store_name . '%'];
                    } else {
                        $where1[] = ['stores.store_name', 'like', '%' . $store_name . '%'];
                    }
                    $obj->where($where1)
                        ->where(['pingan_stores.type' => 1])
                        ->whereIn('stores.user_id', $this->getSubIds(1))
                        ->select('stores.*', 'users.name as user_name')
                        ->orderBy('stores.updated_at', 'desc')
                        ->get();
                } else {
                    $where[] = ['stores.ascription', '=', 1];
                    $obj->where($where)
                        ->where(['pingan_stores.type' => 1])
                        ->whereIn('stores.user_id', $this->getSubIds(1))
                        ->select('stores.*', 'users.name as user_name')
                        ->orderBy('stores.updated_at', 'desc')
                        ->get();
                }
            }else if($user_id == 1002){
                $where[] = ['stores.ascription', '=', 6];
                if ($store_name) {
                    if (is_numeric($store_name)) {
                        $where1[] = ['stores.store_id', 'like', '%' . $store_name . '%'];
                    } else {
                        $where1[] = ['stores.store_name', 'like', '%' . $store_name . '%'];
                    }
                    $obj->where($where1)
                        ->whereIn('stores.user_id', $this->getSubIds(1))
                        ->select('stores.*', 'users.name as user_name')
                        ->orderBy('stores.updated_at', 'desc')
                        ->get();

                } else {
                    $where[] = ['stores.ascription', '=', 6];
                    $obj->where($where)
                        ->whereIn('stores.user_id', $this->getSubIds(1))
                        ->select('stores.*', 'users.name as user_name')
                        ->orderBy('stores.updated_at', 'desc')
                        ->get();
                }
            }else{
                if ($store_name) {
                    if (is_numeric($store_name)) {
                        $where1[] = ['stores.store_id', 'like', '%' . $store_name . '%'];
                    } else {
                        $where1[] = ['stores.store_name', 'like', '%' . $store_name . '%'];
                    }
                    $obj->where($where1)
                        ->whereIn('stores.user_id', $this->getSubIds($user_id))
                        ->select('stores.*', 'users.name as user_name')
                        ->orderBy('stores.updated_at', 'desc')
                        ->get();

                } else {
                    $obj->where($where)
                        ->whereIn('stores.user_id', $this->getSubIds($user_id))
                        ->select('stores.*', 'users.name as user_name')
                        ->orderBy('stores.updated_at', 'desc')
                        ->get();
                }
            }


            $this->t = $obj->count();
            $data = $this->page($obj)->get();

            $this->status = 1;
            $this->message = '数据返回成功';
            return $this->format($data);


        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }

    }

        /**
     * 32位随机数
     * @return string
     */
    public function nonceStr()
    {
        return md5(mt_rand(1000, 9999).time());
    }

    /**
	 * 计算签名，方式一
	 */
	public function signature($params = [])
	{
		return bin2hex(hash('SHA256',$params['appId'].$params['timestamp'].$params['nonce'].'775481e2556e4564985f5439a5e6a277',true));
	}

	public function curl_merch($url,$method,$params,$header = []){
        //初始化CURL句柄
        $curl = curl_init();

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if(!empty($header)){
            curl_setopt ( $curl, CURLOPT_HTTPHEADER, $header );
        }
        //请求时间
        $timeout = 30;
        curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
        switch ($method){
            case "GET" :
                curl_setopt($curl, CURLOPT_HTTPGET, true);
                break;
            case "POST":
                if(is_array($params)){
                    $params = json_encode($params,320);
                }

                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
                curl_setopt($curl, CURLOPT_POSTFIELDS,$params);
                break;
        }
        $data = curl_exec($curl);
        $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        curl_close($curl);//关闭cURL会话

        return $data;
    }

            /**
     * http的curl 方法post请求接口
     * @param string $url
     * @param string $post_data
     * @return string
     */
    function http_curl($url, $post_data)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1); // post数据
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); // post的变量
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($post_data)
            )
        );
        $output = curl_exec($ch);
        curl_close($ch);

        //返回数据
        return $output;
    }


    /**
     * 门店列表（新）
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store_lists_new(Request $request)
    {
        try {
            $user = $this->parseToken();
            $user_id = $request->get('user_id', $user->user_id);
            $time_start = $request->get('time_start', '');//关键词
            $time_end = $request->get('time_end', '');//关键词
            $store_name = $request->get('store_name', '');//关键词
            $province_code = $request->get('province_code');//状态
            $city_code = $request->get('city_code');//状态
            $area_code = $request->get('area_code');//状态
            $status = $request->get('status');//状态
            $pid = $request->get('pid', 0);//主店ID
            $is_delete = $request->get('is_delete', 0);//
            $is_close = $request->get('is_close', 0);//

            $where = [];

            $where[] = ['stores.pid', '=', $pid];
            $where[] = ['stores.is_delete', '=', $is_delete];
            $where[] = ['stores.is_close', '=', $is_close];

            if ($status) {
                $where[] = ['stores.status', '=', $status];
            }
            if ($province_code) {
                $where[] = ['stores.province_code', '=', $province_code];
            }
            if ($city_code) {
                $where[] = ['stores.city_code', '=', $city_code];
            }
            if ($area_code) {
                $where[] = ['stores.area_code', '=', $area_code];
            }
            if ($time_start) {
                $time_start = date('Y-m-d 00:00:00', strtotime($time_start));
                $where[] = ['stores.created_at', '>=', $time_start];
            }
            if ($time_end) {
                $time_end = date('Y-m-d 23:59:59', strtotime($time_end));
                $where[] = ['stores.created_at', '<=', $time_end];
            }

            if (env('DB_D1_HOST')) {
                $obj = DB::connection("mysql_d1")->table("stores");
            } else {
//                $obj = DB::table('stores');
                $obj = new Store();
            }

            $obj = $obj->join('users', 'stores.user_id', 'users.id');

            if ($store_name) {
                if (is_numeric($store_name)) {
                    $where1[] = ['stores.store_id', 'like', '%' . $store_name . '%'];
                } else {
                    $where1[] = ['stores.store_name', 'like', '%' . $store_name . '%'];
                }
                $obj->where($where1)
                    ->whereIn('stores.user_id', $this->getSubIds($user_id))
                    ->with('store_pay_ways')
                    ->select('stores.*', 'users.name as user_name')
                    ->orderBy('stores.updated_at', 'desc');
            } else {
                $obj->where($where)
                    ->whereIn('stores.user_id', $this->getSubIds($user_id))
                    ->with('store_pay_ways')
                    ->select('stores.*', 'users.name as user_name')
                    ->orderBy('stores.updated_at', 'desc');
            }

            $this->t = $obj->count();
            $data = $this->page($obj)->get();
            $data=json_decode($data,true);

            foreach($data as $k=>$v){
                $store_pay_ways=DB::table('store_pay_ways')->where('store_id', $v['store_id'])->where('ways_source', 'weixin')->orderBy('sort', 'ASC')->get();
                foreach ($store_pay_ways as $key=>$val){
                    if($val->company=='vbill'){
                        $val->pay_amount_e='https://pay.zgjindoufu.com/upload/fuwushang/suixingfu-tongdao@2x.png';
                    }else if($val->company=='alipay'){
                        $val->pay_amount_e='https://pay.zgjindoufu.com/upload/fuwushang/zhifubao-tongdao@2x.png';
                    }else if($val->company=='weixin'){
                        $val->pay_amount_e='https://pay.zgjindoufu.com/upload/fuwushang/weixin-tongdao@2x.png';
                    }else if($val->company=='lianfu'){
                        $val->pay_amount_e='https://pay.zgjindoufu.com/upload/fuwushang/lianfu.jpg';
                    }else if($val->company=='lianfuyoupay'){
                        $val->pay_amount_e='https://pay.zgjindoufu.com/upload/fuwushang/lianfuyoupay.jpg';
                    }else if($val->company=='linkage'){
                        $val->pay_amount_e='https://pay.zgjindoufu.com/upload/fuwushang/linkage.jpg';
                    }else if($val->company=='wftpay'){
                        $val->pay_amount_e='https://pay.zgjindoufu.com/upload/fuwushang/wftpay.jpg';
                    }else if($val->company=='hwcpay'){
                        $val->pay_amount_e='https://pay.zgjindoufu.com/upload/fuwushang/hwcpay.jpg';
                    }else if($val->company=='nongjin'){
                        $val->pay_amount_e='https://pay.zgjindoufu.com/upload/fuwushang/nongjin.jpg';
                    }else if($val->company=='abc'){
                        $val->pay_amount_e='https://pay.zgjindoufu.com/upload/fuwushang/abc.jpg';
                    }else if($val->company=='unionpay_merch'){
                        $val->pay_amount_e='https://pay.zgjindoufu.com/upload/fuwushang/unionpay_merch.jpg';
                    }else if($val->company=='wingpay'){
                        $val->pay_amount_e='https://pay.zgjindoufu.com/upload/fuwushang/wingpay.jpg';
                    }else if($val->company=='saobei'){
                        $val->pay_amount_e='https://pay.zgjindoufu.com/upload/fuwushang/saobei.jpg';
                    }else if($val->company=='abc_quiet'){
                        $val->pay_amount_e='https://pay.zgjindoufu.com/upload/fuwushang/abc_quiet.jpg';
                    }else if($val->company=='cool'){
                        $val->pay_amount_e='https://pay.zgjindoufu.com/upload/fuwushang/cool.jpg';
                    }else if($val->company=='pingan'){
                        $val->pay_amount_e='https://pay.zgjindoufu.com/upload/fuwushang/pingan.jpg';
                    }else if($val->company=='jiujiang'){
                        $val->pay_amount_e='https://pay.zgjindoufu.com/upload/fuwushang/jiujiang.jpg';
                    }else if($val->company=='ccb'){
                        $val->pay_amount_e='https://pay.zgjindoufu.com/upload/fuwushang/ccb.jpg';
                    }else if($val->company=='unionpay'){
                        $val->pay_amount_e='https://pay.zgjindoufu.com/upload/fuwushang/yunshanfu-tongdao@2x.png';
                    }else if($val->company=="cmbc"){
                        $val->pay_amount_e='https://pay.zgjindoufu.com/upload/fuwushang/cmbc1.jpg';
                    }else if($val->company=="jdjr"){
                        $val->pay_amount_e='https://pay.zgjindoufu.com/upload/fuwushang/jingdongzhifu.png';
                    }else if($val->company=="mybank"){
                        $val->pay_amount_e='https://pay.zgjindoufu.com/upload/fuwushang/kuaiqian.jpg';
                    }else if($val->company=="cib"){
                        $val->pay_amount_e='https://pay.zgjindoufu.com/upload/fuwushang/xingye1.jpg';
                    }else if($val->company=="yinsheng"){
                        $val->pay_amount_e='https://pay.zgjindoufu.com/upload/fuwushang/yinsheng.jpg';
                    }else if($val->company=="easypay"){
                        $val->pay_amount_e='https://pay.zgjindoufu.com/upload/fuwushang/ys.jpg';
                    }else if($val->company=="easypaya"){
                        $val->pay_amount_e='https://pay.zgjindoufu.com/upload/fuwushang/ys.jpg';
                    }
                }
                $data[$k]['store_pay_ways']=$store_pay_ways;
                $rate=DB::table('store_pay_ways')->where('store_id', $v['store_id'])->where('ways_source', 'weixin')->orderBy('sort', 'asc')->first();
                if(!empty($rate)){
                    $data[$k]['rate']=$rate->rate;
                }else{
                    $data[$k]['rate']='0.00';
                }
            }


            $this->status = 1;
            $this->message = '数据返回成功';
            return $this->format($data);


        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }

    }


    /**
     * 列表 根据门店ID 获取所有门店名称
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store_all_lists(Request $request)
    {
        try {
            $user = $this->parseToken();
            $store_id = $request->get('store_id', '');//
            $id = $request->get('id', '');//
            $l = $request->get('l', '50');


            if (env('DB_D1_HOST')) {
                $obj = DB::connection("mysql_d1")->table("stores");
            } else {
                $obj = DB::table('stores');
            }

            $obj->orWhere('id', $id)
                ->orWhere('pid', $id)
                ->select('store_id', 'store_short_name');

            $this->t = $obj->count();
            $data = $this->page($obj)->get();

            $this->status = 1;
            $this->message = '数据返回成功';
            return $this->format($data);


        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }

    }


    /**
     * 列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store_pc_lists(Request $request)
    {
        try {
            $user = $this->parseToken();
            $user_id = $request->get('user_id', $user->user_id);
            $time_start = $request->get('time_start', '');//关键词
            $time_end = $request->get('time_end', '');//关键词
            $store_name = $request->get('store_name', '');//关键词
            $province_code = $request->get('province_code');//状态
            $city_code = $request->get('city_code');//状态
            $area_code = $request->get('area_code');//状态
            $status = $request->get('status');//状态
            $pid = $request->get('pid', 0);//主店ID
            $is_delete = $request->get('is_delete', 0);//
            $is_close = $request->get('is_close', 0);//
            $is_incoming = $request->get('is_incoming', 2);//
            $is_quota = $request->get('is_quota',0);//
            $phone = $request->get('phone', '');

            $worker_phone=$request->get("worker_phone","");

            $where = [];
            $where2 = [];
            $where3 = [];

            if($phone){
                $where2[] = ['stores.people_phone', 'like', '%' . $phone . '%'];
                $where3[] = ['stores.people', 'like', '%' . $phone . '%'];
            }

            $where[] = ['stores.pid', '=', $pid];
            if($is_delete=='2'){
            }else{
                $where[] = ['stores.is_delete', '=', $is_delete];
            }

            if($is_incoming=='2'){
            }else{
                $where[] = ['stores.is_incoming', '=', $is_incoming];
            }
            if($is_quota == '0'){
            }else{
                $where[] = ['stores.is_quota', '=', $is_quota];
            }

            if($is_close=='2'){
            }else{
                $where[] = ['stores.is_close', '=', $is_close];
            }

            if ($status) {
                $where[] = ['stores.status', '=', $status];
            }

            $store_short_name = $request->get('store_short_name', '');
            if ($store_short_name) {
//                $where[] = ['stores.store_short_name', '=', $store_short_name];
                $where[] = ['stores.store_short_name', 'like', '%' . $store_short_name . '%'];
            }

            if ($province_code) {
                $where[] = ['stores.province_code', '=', $province_code];
            }
            if ($city_code) {
                $where[] = ['stores.city_code', '=', $city_code];
            }
            if ($area_code) {
                $where[] = ['stores.area_code', '=', $area_code];
            }

            if ($time_start) {
                $time_start = date('Y-m-d 00:00:00', strtotime($time_start));
                $where[] = ['stores.created_at', '>=', $time_start];
            }
            if ($time_end) {
                $time_end = date('Y-m-d 23:59:59', strtotime($time_end));
                $where[] = ['stores.created_at', '<=', $time_end];
            }

            if (env('DB_D1_HOST')) {
                $obj = DB::connection("mysql_d1")->table("stores");
            } else {
                $obj = DB::table('stores');
            }

            if($worker_phone){
                $worker=ExtensionWorkers::query()->where("phone",$worker_phone)->first();
                $store_ids=ExtensionWorkerStores::query()->where("worker_id",empty($worker)?-1:$worker->id)->pluck("store_id")->toArray();
                $obj->whereIn('stores.store_id', $store_ids);
            }

            if($user_id==101){
                $obj = $obj->join('users', 'stores.user_id', 'users.id');
                if ($store_name) {
                    if (is_numeric($store_name)) {
                        $where1[] = ['stores.store_id', 'like', '%' . $store_name . '%'];
                    } else {
                        $where1[] = ['stores.store_name', 'like', '%' . $store_name . '%'];
                    }

                    $obj->where($where1)
                        ->whereIn('stores.user_id', $this->getSubIds(1))
                        ->orderBy('stores.created_at', 'desc')
                        ->select('stores.*', 'users.name as user_name')
                        ->get();

                } else {
                    $where[] = ['stores.ascription', '=', 1];
                    $obj->where($where2)
                        ->orWhere($where3)
                        ->where($where)
                        ->whereIn('stores.user_id', $this->getSubIds(1))
                        ->orderBy('stores.created_at', 'desc')
                        ->select('stores.*', 'users.name as user_name')
                        ->get();
                }
            } else if($user_id == 747){
                $obj = $obj->join('users', 'stores.user_id', 'users.id');
                if ($store_name) {
                    $where1[] = ['stores.ascription', '=', 4];
                    if (is_numeric($store_name)) {
                        $where1[] = ['stores.store_id', 'like', '%' . $store_name . '%'];
                    } else {
                        $where1[] = ['stores.store_name', 'like', '%' . $store_name . '%'];
                    }

                    $obj->where($where1)
                        ->whereIn('stores.user_id', $this->getSubIds(1))
                        ->orderBy('stores.created_at', 'desc')
                        ->select('stores.*', 'users.name as user_name')
                        ->get();

                } else {
                    $where[] = ['stores.ascription', '=', 4];
                    $obj->where($where2)
                        ->orWhere($where3)
                        ->where($where)
                        ->whereIn('stores.user_id', $this->getSubIds(1))
                        ->orderBy('stores.created_at', 'desc')
                        ->select('stores.*', 'users.name as user_name')
                        ->get();
                }
            } else if($user_id == 874){
                $obj = $obj->join('users', 'stores.user_id', 'users.id');
                if ($store_name) {
                    $where1[] = ['stores.ascription', '=', 5];
                    if (is_numeric($store_name)) {
                        $where1[] = ['stores.store_id', 'like', '%' . $store_name . '%'];
                    } else {
                        $where1[] = ['stores.store_name', 'like', '%' . $store_name . '%'];
                    }

                    $obj->where($where1)
                        ->whereIn('stores.user_id', $this->getSubIds(1))
                        ->orderBy('stores.created_at', 'desc')
                        ->select('stores.*', 'users.name as user_name')
                        ->get();

                } else {
                    $where[] = ['stores.ascription', '=', 5];
                    $obj->where($where2)
                        ->orWhere($where3)
                        ->where($where)
                        ->whereIn('stores.user_id', $this->getSubIds(1))
                        ->orderBy('stores.created_at', 'desc')
                        ->select('stores.*', 'users.name as user_name')
                        ->get();
                }
            } else if($user_id == 1002){
                $obj = $obj->join('users', 'stores.user_id', 'users.id');
                if ($store_name) {
                    $where1[] = ['stores.ascription', '=', 6];
                    if (is_numeric($store_name)) {
                        $where1[] = ['stores.store_id', 'like', '%' . $store_name . '%'];
                    } else {
                        $where1[] = ['stores.store_name', 'like', '%' . $store_name . '%'];
                    }

                    $obj->where($where1)
                        ->whereIn('stores.user_id', $this->getSubIds(1))
                        ->orderBy('stores.created_at', 'desc')
                        ->select('stores.*', 'users.name as user_name')
                        ->get();

                } else {
                    $where[] = ['stores.ascription', '=', 6];
                    $obj->where($where2)
                        ->orWhere($where3)
                        ->where($where)
                        ->whereIn('stores.user_id', $this->getSubIds(1))
                        ->orderBy('stores.created_at', 'desc')
                        ->select('stores.*', 'users.name as user_name')
                        ->get();
                }
            }else if($user_id == 347){ // 平安使用插件标记
                $obj = DB::table('pingan_stores')->join('stores', 'pingan_stores.store_id', 'stores.store_id')->join('users', 'stores.user_id', 'users.id');
                if ($store_name) {
                    if (is_numeric($store_name)) {
                        $where1[] = ['stores.store_id', 'like', '%' . $store_name . '%'];
                    } else {
                        $where1[] = ['stores.store_name', 'like', '%' . $store_name . '%'];
                    }
                    $obj->where($where1)
                        ->where(['pingan_stores.type' => 1])
                        ->whereIn('stores.user_id', $this->getSubIds(1))
                        ->orderBy('stores.created_at', 'desc')
                        ->select('stores.*', 'users.name as user_name')
                        ->get();
                } else {
                    $obj->where($where2)
                        ->orWhere($where3)
                        ->where($where)
                        ->where(['pingan_stores.type' => 1])
                        ->whereIn('stores.user_id', $this->getSubIds(1))
                        ->orderBy('stores.created_at', 'desc')
                        ->select('stores.*', 'users.name as user_name')
                        ->get();
                }
            }else{
                $obj = $obj->join('users', 'stores.user_id', 'users.id');
                if ($store_name) {
                    if (is_numeric($store_name)) {
                        $where1[] = ['stores.store_id', 'like', '%' . $store_name . '%'];
                    } else {
                        $where1[] = ['stores.store_name', 'like', '%' . $store_name . '%'];
                    }

                    $obj->where($where1)
                        ->whereIn('stores.user_id', $this->getSubIds($user_id))
                        ->orderBy('stores.created_at', 'desc')
                        ->select('stores.*', 'users.name as user_name')
                        ->get();

                } else {
                    $obj->where($where2)
                        ->orWhere($where3)
                        ->where($where)
                        ->whereIn('stores.user_id', $this->getSubIds($user_id))
                        ->orderBy('stores.created_at', 'desc')
                        ->select('stores.*', 'users.name as user_name')
                        ->get();
                }
            }


            $this->t = $obj->count();
            $data = $this->page($obj)->get();
            foreach ($data as $k=>$v){
                $p_store=DB::table('stores')->where('pid', $v->id)->first();

                $v->p_info = null;
                if ($v->pid > 0) {
                    $v->p_info =DB::table('stores')->select('store_name','store_id')->where('id', $v->pid)->first();
                }
                $v->store_btn = DB::table('store_goods_btn')->where('store_id',$v->store_id)->first();

                if(!$p_store){
                    $v->branch='无';
                }else{
                    $v->branch='有';
                }
            }
            $this->status = 1;

            $this->message = '数据返回成功'.$user_id;
            return $this->format($data);


        } catch (\Exception $exception) {
            Log::info($exception);
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }

    }


    /**
     * 门店信息查看
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request)
    {
        try {
            $user = $this->parseToken();
            $store_id = $request->get('store_id', '');
            $store = Store::where('store_id', $store_id)->first();
            $store_bank = StoreBank::where('store_id', $store_id)->first();
            $store_img = StoreImg::where('store_id', $store_id)->first();
            if (!$store) {
                return json_encode([
                    'status' => 2,
                    'message' => '门店未认证'
                ]);

            }

            $store_alipay_account = '';
            $AlipayAppOauthUsers = AlipayAppOauthUsers::where('store_id', $store_id)->first();
            if ($AlipayAppOauthUsers) {
                $store_alipay_account = $AlipayAppOauthUsers->alipay_user_account;
            }

            if($store->category_id>=416){
                $category=DB::table('vbill_store_category')->where('mcc', $store->category_id)->first();
                $store->category_id=$category->id;
                $store->category_name=$category->category_name;
            }

            $type = $request->get('type');
            if ($type == "") {
                $type = 'head_info,store_info,account_info,license_info';
            }
            $type_array = explode(",", $type);
            foreach ($type_array as $k => $v) {
                if ($v == 'head_info') {
                    $data = [
                        'head_name' => $store->head_name,//法人
                        'head_sfz_no' => $store->head_sfz_no,
                        'head_sfz_img_a' => $store_img->head_sfz_img_a,
                        'head_sfz_img_b' => $store_img->head_sfz_img_b,
                        'people' => $store->people,
                        'people_phone' => $store->people_phone,
                        'head_sfz_time' => $store->head_sfz_time,
                        'head_sfz_stime' => $store->head_sfz_stime,
                    ];

                    $data_r[$v] = $data;
                }

                if ($v == 'store_info') {
                    $data = [
                        'people' => $store->people,
                        'people_phone' => $store->people_phone,
                        'store_email' => $store->store_email,
                        'store_name' => $store->store_name,
                        'store_short_name' => $store->store_short_name,
                        'province_code' => $store->province_code,
                        'city_code' => $store->city_code,
                        'area_code' => $store->area_code,
                        'province_name' => $store->province_name,
                        'city_name' => $store->city_name,
                        'area_name' => $store->area_name,
                        'store_address' => $store->store_address,
                        'store_type' => $store->store_type,
                        'store_type_name' => $store->store_type_name,
                        'category_id' => $store->category_id,
                        'category_name' => $store->category_name,
                        'store_logo_img' => $store_img->store_logo_img,
                        'store_img_a' => $store_img->store_img_a,
                        'store_img_a_people' => $store_img->store_img_a_people,
                        'store_img_b' => $store_img->store_img_b,
                        'store_img_c' => $store_img->store_img_c,
                        'status' => $store->status,
                        'status_desc' => $store->status_desc,
                        'alipay_name' => $store->alipay_name,
                        'alipay_account' => $store->alipay_account,

                    ];

                    $data_r[$v] = $data;
                }

                if ($v == 'account_info') {
                    $data = [
                        'store_alipay_account' => $store_alipay_account,
                        'store_bank_no' => $store_bank->store_bank_no,
                        'store_bank_phone' => $store_bank->store_bank_phone,
                        'store_bank_name' => $store_bank->store_bank_name,
                        'store_bank_type' => $store_bank->store_bank_type,
                        'bank_name' => $store_bank->bank_name,
                        'bank_no' => $store_bank->bank_no,
                        'sub_bank_name' => $store_bank->sub_bank_name,
                        'bank_province_code' => $store_bank->bank_province_code,
                        'bank_city_code' => $store_bank->bank_city_code,
                        'bank_area_code' => $store_bank->bank_area_code,
                        'bank_province_name' => $this->city_name($store_bank->bank_province_code),
                        'bank_city_name' => $this->city_name($store_bank->bank_city_code),
                        'bank_area_name' => $this->city_name($store_bank->bank_area_code),
                        'bank_img_a' => $store_img->bank_img_a,
                        'bank_img_b' => $store_img->bank_img_b,
                        'bank_sfz_img_a' => $store_img->bank_sfz_img_a,
                        'bank_sfz_img_b' => $store_img->bank_sfz_img_b,
                        'bank_sc_img' => $store_img->bank_sc_img,
                        'store_auth_bank_img' => $store_img->store_auth_bank_img,
                        'bank_sfz_time' => $store_bank->bank_sfz_time,
                        'bank_sfz_stime' => $store_bank->bank_sfz_stime,
                        'bank_sfz_no' => $store_bank->bank_sfz_no,
                        'weixin_name' => $store->weixin_name,
                        'weixin_no' => $store->weixin_no,
                        'alipay_name' => $store->alipay_name,
                        'alipay_account' => $store->alipay_account,
                        'store_email' => $store->store_email,

                    ];

                    $data_r[$v] = $data;
                }

                if ($v == 'license_info') {
                    $data = [
                        'store_license_no' => $store->store_license_no,
                        'store_license_time' => $store->store_license_time,
                        'store_license_stime' => $store->store_license_stime,
                        'store_license_img' => $store_img->store_license_img,
                        'store_other_img_a' => $store_img->store_other_img_a,
                        'store_other_img_b' => $store_img->store_other_img_b,
                        'store_other_img_c' => $store_img->store_other_img_c,
                        'store_industrylicense_img' => $store_img->store_industrylicense_img,
                        'head_sc_img' => $store_img->head_sc_img,
                        'head_store_img' => $store_img->head_store_img,
                    ];

                    $data_r[$v] = $data;
                }
            }

            $this->status = 1;
            $this->message = '成功';
            return $this->format($data_r);

        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage() . '-' . $exception->getLine();
            return $this->format();
        }
    }


    /**
     * 门店认证修改
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|string
     */
    public function up_store(Request $request)
    {
        try {
            $token = $this->parseToken();
            $user_login_id = $token->user_id;
            $data = $request->except(['token']);

            $hkrt_bus_scope_code = $request->post('hkrt_bus_scope_code', ''); //经营范围
            $reserved_mobile = $request->post('reserved_mobile', ''); //预留手机号

            $store_id = date('Ymdhis', time()) . rand(1000, 9999);//随机
            $store_id = $request->get('store_id', $store_id);
            $config_id = $token->config_id;
            $user_id = $token->user_id;
            $user_id = $request->get('user_id', $user_id);

            //法人信息
            $people_phone = $request->get('people_phone', '');
            $store_email = $request->get('store_email', '');
            $head_name = $request->get('head_name', '');
            $head_sfz_no = $request->get('head_sfz_no', '');
            $bank_sfz_no = $request->get('bank_sfz_no', '');
            $people = $request->get('people', $head_name);

            $head_sfz_img_a = $request->get('head_sfz_img_a', '');
            $head_sfz_img_b = $request->get('head_sfz_img_b', '');

            $head_sfz_time = $request->get('head_sfz_time', '');
            $head_sfz_stime = $request->get('head_sfz_stime', '');
            $head_sfz_time = $this->time($head_sfz_time);
            $head_sfz_stime = $this->time($head_sfz_stime);

            //门店信息
            $store_name = $request->get('store_name', '');
            $store_short_name = $request->get('store_short_name', $store_name);
            $province_code = $request->get('province_code', '');
            $city_code = $request->get('city_code', '');
            $area_code = $request->get('area_code', '');
            $store_address = $request->get('store_address', '');
            $store_type = $request->get('store_type', '');
            $store_type_name = $request->get('store_type_name', '');
            $dlb_micro_biz_type = $request->get('DLB_micro_biz_type', '');
            $dlb_industry = $request->get('DLB_industry', '');
            $dlb_second_industry = $request->get('DLB_second_industry', '');
            $dlb_province = $request->get('DLB_province', '');
            $dlb_city = $request->get('DLB_city', '');
            $dlb_bank = $request->get('DLB_bank', '');
            $dlb_sub_bank = $request->get('DLB_sub_bank', '');
            $dlb_pay_bank_list = $request->get('DLB_pay_bank_list', '');
            if ($dlb_pay_bank_list) {
                $dlb_pay_bank_list = json_encode($dlb_pay_bank_list);
            }
            $longitude = $request->get('longitude', '');
            $latitude = $request->get('latitude', '');

            $category_id = $request->get('category_id', '');
            $category_name = $request->get('category_name', '');
            $store_logo_img = $request->get('store_logo_img', '');
            $store_img_a = $request->get('store_img_a', '');
            $store_img_a_people=$request->get('store_img_a_people', '');
            $store_img_b = $request->get('store_img_b', '');
            $store_img_c = $request->get('store_img_c', '');
            $weixin_name = $request->get('weixin_name', '');
            $weixin_no = $request->get('weixin_no', '');
            $alipay_name = $request->get('alipay_name', '');
            $alipay_account = $request->get('alipay_account', '');

            //收款信息
            $store_alipay_account = $request->get('store_alipay_account', '');
            $store_bank_no = $request->get('store_bank_no', '');
            $store_bank_phone = $request->get('store_bank_phone', '');
            $store_bank_name = $request->get('store_bank_name', '');
            $store_bank_type = $request->get('store_bank_type', '');
            $bank_name = $request->get('bank_name', '');
            $bank_no = $request->get('bank_no', '');
            $sub_bank_name = $request->get('sub_bank_name', '');
            $bank_province_code = $request->get('bank_province_code', '');
            $bank_city_code = $request->get('bank_city_code', '');
            $bank_area_code = $request->get('bank_area_code', '');
            $bank_img_a = $request->get('bank_img_a', '');
            $bank_img_b = $request->get('bank_img_b', '');

            $bank_sfz_img_a = $request->get('bank_sfz_img_a', '');
            $bank_sfz_img_b = $request->get('bank_sfz_img_b', '');
            $bank_sc_img = $request->get('bank_sc_img', '');
            $store_auth_bank_img = $request->get('store_auth_bank_img', '');

            $bank_sfz_time = $request->get('bank_sfz_time', '');
            $bank_sfz_stime = $request->get('bank_sfz_stime', '');
            $bank_sfz_time = $this->time($bank_sfz_time);
            $bank_sfz_stime = $this->time($bank_sfz_stime);

            //证照信息
            $store_license_no = $request->get('store_license_no', '');
            $store_license_time = $request->get('store_license_time', '');
            $store_license_stime = $request->get('store_license_stime', '');

            $head_sc_img = $request->get('head_sc_img', '');
            $head_store_img = $request->get('head_store_img', '');

            $store_license_img = $request->get('store_license_img', '');
            $store_industrylicense_img = $request->get('store_industrylicense_img', '');
            $store_other_img_a = $request->get('store_other_img_a', '');
            $store_other_img_b = $request->get('store_other_img_b', '');
            $store_other_img_c = $request->get('store_other_img_c', '');
            $dlb_settler_hold_settlecard = $request->get('DLB_settler_hold_settlecard', ''); //哆啦宝-结算人手持结算卡

            $ystore = Store::where('store_id', $store_id)->first();
            if(!empty($ystore) && empty($ystore->store_name) && empty($store_name)){
                return json_encode([
                    'status' => 2,
                    'message' => '请先提交门店信息资料'
                ]);
            }
            //拼装门店信息
            $stores = [
                'config_id' => $config_id,
                'user_id' => $user_id,
                'merchant_id' => '',
                'store_id' => $store_id,
                'store_name' => $store_name,
                'store_type_name' => $store_type_name,
                'store_email' => $store_email,
                'store_short_name' => $store_short_name,
                'people' => $people, //
                'people_phone' => $people_phone,
                'province_code' => $province_code,
                'city_code' => $city_code,
                'area_code' => $area_code,
                'store_address' => $store_address,
                'head_name' => $head_name, //法人
                'head_sfz_no' => $head_sfz_no,
                'head_sfz_time' => $head_sfz_time,
                'head_sfz_stime' => $head_sfz_stime,
                'category_id' => $category_id,
                'category_name' => $category_name,
                'weixin_name' => isset($weixin_name) ? $weixin_name : "",
                'weixin_no' => isset($weixin_no) ? $weixin_no : "",
                'alipay_name' => isset($alipay_name) ? $alipay_name : "",
                'alipay_account' => isset($alipay_account) ? $alipay_account : ""
            ];
            if ($store_license_no) $stores['store_license_no'] = $store_license_no;
            if ($store_license_time) $stores['store_license_time'] = $store_license_time;
            if ($store_license_stime) $stores['store_license_stime'] = $store_license_stime;
            if ($dlb_micro_biz_type) $stores['dlb_micro_biz_type'] = $dlb_micro_biz_type; //哆啦宝-经营类型
            if ($dlb_industry) $stores['dlb_industry'] = $dlb_industry; //哆啦宝店铺一级行业
            if ($dlb_second_industry) $stores['dlb_second_industry'] = $dlb_second_industry; //店铺二级行业
            if ($dlb_province) $stores['dlb_province'] = $dlb_province; //哆啦宝-商户省份
            if ($dlb_city) $stores['dlb_city'] = $dlb_city; //哆啦宝商户城市
            if ($dlb_bank) $stores['dlb_bank'] = $dlb_bank; //哆啦宝-银行名称
            if ($dlb_sub_bank) $stores['dlb_sub_bank'] = $dlb_sub_bank; //哆啦宝-银行分行名称
            if ($dlb_pay_bank_list) $stores['dlb_pay_bank_list'] = $dlb_pay_bank_list; //支付类型+费率
            if ($hkrt_bus_scope_code) $stores['hkrt_bus_scope_code'] = $hkrt_bus_scope_code; //经营范围
            if ($store_type) {
                $stores['store_type'] = $store_type;
            }
            if ($province_code) {
                $stores['province_name'] = $this->city_name($province_code);
            }
            if ($city_code) {
                $stores['city_name'] = $this->city_name($city_code);
            }
            if ($area_code) {
                $stores['area_name'] = $this->city_name($area_code);
            }
            if ($longitude) $stores['lng'] = $longitude; //经度
            if ($latitude) $stores['lat'] = $latitude; //纬度

            //图片信息
            $store_imgs = [
                'store_id' => $store_id,
                'head_sfz_img_a' => $head_sfz_img_a,
                'head_sfz_img_b' => $head_sfz_img_b,
                'store_license_img' => $store_license_img,
                'store_industrylicense_img' => $store_industrylicense_img,
                'store_logo_img' => $store_logo_img,
                'store_img_a' => $store_img_a,
                "store_img_a_people"=>$store_img_a_people,
                'store_img_b' => $store_img_b,
                'store_img_c' => $store_img_c,
                'bank_img_a' => $bank_img_a,
                'bank_img_b' => $bank_img_b,
                'store_other_img_a' => $store_other_img_a,
                'store_other_img_b' => $store_other_img_b,
                'store_other_img_c' => $store_other_img_c,
                'head_sc_img' => $head_sc_img,
                'head_store_img' => $head_store_img,
                'bank_sfz_img_a' => $bank_sfz_img_a,
                'bank_sfz_img_b' => $bank_sfz_img_b,
                'bank_sc_img' => $bank_sc_img,
                'store_auth_bank_img' => $store_auth_bank_img
            ];
            if ($dlb_settler_hold_settlecard) $store_imgs['dlb_settler_hold_settlecard'] = $dlb_settler_hold_settlecard; //哆啦宝-结算人手持结算卡

            //银行卡信息
            $store_banks = [
                'store_id' => $store_id,
                'store_bank_no' => $store_bank_no,
                'store_bank_name' => $store_bank_name,
                'store_bank_phone' => $store_bank_phone,
                'store_bank_type' => $store_bank_type,
                'bank_name' => $bank_name,
                'bank_no' => $bank_no,
                'sub_bank_name' => $sub_bank_name,
                'bank_province_code' => $bank_province_code,
                'bank_city_code' => $bank_city_code,
                'bank_area_code' => $bank_area_code,
                'bank_sfz_no' => $bank_sfz_no,//持卡人默认是法人
                'bank_sfz_time' => $bank_sfz_time,
                'bank_sfz_stime' => $bank_sfz_stime
            ];
            if ($reserved_mobile) $store_banks['reserved_mobile'] = $reserved_mobile; //海科融通结算卡预留手机号

            $store = Store::where('store_id', $store_id)->first();
            $store_bank = StoreBank::where('store_id', $store_id)->first();
            $store_img = StoreImg::where('store_id', $store_id)->first();

            if ($store) {
                $is_store = 1;
            } else {
                $is_store = 0;
            }

            $is_merchant = 0;
            //判断手机号是否被注册
            if ($people_phone) {
                //验证手机号
                if (!preg_match("/^1[3456789]{1}\d{9}$/", $people_phone)) {
                    return json_encode([
                        'status' => 2,
                        'message' => '手机号码格式不正确'
                    ]);
                }

                $Merchant = Merchant::where('phone', $people_phone)
                    ->select('id')->first();
                if ($Merchant) {
                    $is_merchant = 1;
                }

                if ($is_merchant == 1 && $is_store == 0) {
                    return json_encode([
                        'status' => 2,
                        'message' => '手机号已被注册,请更换'
                    ]);
                }
            }

            //提交的新商户必须填写
            if (!$store) {
                $check_data = [
                    'store_id' => '门店ID'
                ];
                $check = $this->check_required($request->except(['token']), $check_data);
                if ($check) {
                    return json_encode([
                        'status' => 2,
                        'message' => $check
                    ]);
                }
            }

            //开启事务
            try {
                DB::beginTransaction();

                //入库账户
                if ($people_phone) {
                    //未注册
                    if ($is_merchant == 0) {
                        //注册账户
                        $dataIN = [
                            'pid' => 0,
                            'type' => 1,
                            'name' => $people,
                            'email' => '',
                            'password' => bcrypt('000000'),
                            'pay_password' => bcrypt('000000'),
                            'phone' => $people_phone,
                            'user_id' => $user_id, //推广员id
                            'config_id' => $config_id,
                            'wx_openid' => ''
                        ];
                        $merchant = Merchant::create($dataIN);
                        $merchant_id = $merchant->id;

                        $stores['merchant_id'] = $merchant_id;
                        MerchantStore::create([
                            'store_id' => $store_id,
                            'merchant_id' => $merchant_id
                        ]);
                    }
                }

                if ($store) {
                    //修改   不修改归属ID
                    $stores['user_id'] = $store->user_id;
                    $stores['config_id'] = $store->config_id;
                    $stores = array_filter($stores, function ($v) {
                        if ($v == "") {
                            return false;
                        } else {
                            return true;
                        }
                    });
                    $store->update($stores);
                    $store->save();
                } else {
                    $rules = [
                        'store_name' => 'unique:stores',
                    ];
                    $validator = Validator::make($data, $rules);
                    if ($validator->fails()) {
                        return json_encode([
                            'status' => 2,
                            'message' => '门店名称在系统中已经存在',
                        ]);
                    }

                    Store::create($stores);
                }

                if ($store_img) {
                    $store_img->update(array_filter($store_imgs));
                    $store_img->save();
                } else {
                    StoreImg::create($store_imgs);
                }

                //快钱支付修改资料
//                $mybank = StorePayWay::where('store_id', $store_id)
//                    ->where('company', 'mybank')
//                    ->select('status')
//                    ->first();
//                if ($mybank && $mybank->status == 1) {
//                    $MyBankStore = MyBankStore::where('OutMerchantId', $store_id)
//                        ->select('MerchantId')
//                        ->first();
//                    if ($MyBankStore && $MyBankStore->MerchantId) {
//                        //修改银行资料
//                        $obj = new MyBankController();
//                        if ($store_bank_name && $store_bank_no && $bank_no) {
//                            $BankCardParam = [
//                                "config_id" => $config_id,
//                                "MerchantId" => $MyBankStore->MerchantId,
//                                "BankCertName" => $store_bank_name,//名称
//                                "BankCardNo" => $store_bank_no,//银行卡号
//                                "AccountType" => $store_bank_type ? $store_bank_type : '01',//账户类型。可选值：01：对私账 02对公账户
//                                "BankCode" => $bank_name,//开户行名称
//                                "BranchName" => $sub_bank_name,//开户支行名称
//                                "ContactLine" => $bank_no,//联航号
//                                "BranchProvince" => $bank_province_code,//省编号
//                                "BranchCity" => $bank_city_code,//市编号
//                                "CertType" => '01',//持卡人证件类型。可选值： 01：身份证
//                                "CertNo" => $store_bank->bank_sfz_no ? $store_bank->bank_sfz_no : $store->head_sfz_no,//持卡人证件号码
//                                "CardHolderAddress" => $store->store_address,//持卡人地址
//                            ];
//
//                            $re = $obj->up_bank($BankCardParam);
//                            if ($re['status'] == 2) {
//                                return json_encode($re);
//                            }
//
//                            $body = $re['data']['document']['response']['body'];
//                            if ($body['RespInfo']['ResultStatus'] != 'S') {
//                                return json_encode([
//                                    'status' => 2,
//                                    'message' => '换卡失败：' . $body['RespInfo']['ResultMsg']
//                                ]);
//                            }
//                        }
//                    }
//                }

                //传化修改资料
//                $tfpay = StorePayWay::where('store_id', $store_id)
//                    ->where('company', 'tfpay')
//                    ->select('status')
//                    ->first();
//                if ($token->pid == "0" && $tfpay && $tfpay->status == 1) {
//                    if ($store_bank_name && $store_bank_no && $bank_no) {
//                        $config = new TfConfigController();
//
//                        $tf_merchant = $config->tf_merchant($store_id, $store->pid);
//                        if (!$tf_merchant) {
//                            return json_encode([
//                                'status' => 2,
//                                'message' => '传化商户不存在请检查'
//                            ]);
//                        }
//
//                        $tf_config = $config->tf_config($config_id, $tf_merchant->qd);
//                        if (!$tf_config) {
//                            return json_encode([
//                                'status' => 2,
//                                'message' => '传化配置不存在请检查配置'
//                            ]);
//                        }
//
//                        //传化报备
//                        $tfpay_banks = DB::table('tfpay_banks')
//                            ->where('bank_name', $bank_name)
//                            ->first();
//                        if (!$tfpay_banks) {
//                            return json_encode([
//                                'status' => 2,
//                                'message' => '暂不支持该银行卡进件'
//                            ]);
//                        }
//
//                        $obj = new \App\Api\Controllers\Tfpay\BaseController();
//                        $obj->mch_id = $tf_config->mch_id;
//                        $obj->pub_key = $tf_config->pub_key;
//                        $obj->pri_key = $tf_config->pri_key;
//
//                        //费率同步
//                        $sub_mch_id = $tf_merchant->sub_mch_id;
//                        $post_data = [
//                            'sub_mch_id' => $sub_mch_id,
//                            'type' => $store_bank_type == "02" ? '2' : '1',//1 个人 2 企业
//                            'bank_code' => $tfpay_banks->bank_no,
//                            'account_name' => $store_bank_name,
//                            'account_number' => $store_bank_no,
//                            'bank_province_code' => $bank_province_code ? $bank_province_code : $province_code,
//                            'bank_city_code' => $bank_city_code ? $bank_city_code : $city_code,
//                            'branch_name' => $sub_bank_name,
//                        ];
//                        $method = '/openapi/merchant/settle-bankcard';
//                        $re = $obj->api($post_data, $method, false);
//                        if ($re['code'] != 0) {
//                            return json_encode([
//                                'status' => 2,
//                                'message' => $re['msg']
//                            ]);
//                        }
//                    }
//                }

                if ($store_bank) {
                    $store_bank->update(array_filter($store_banks));
                    $store_bank->save();
                } else {
                    $stores['store_type'] = 1;
                    $stores['store_type_name'] = '个体';
                    StoreBank::create($store_banks);
                }

                DB::commit();
            } catch (\Exception $ex) {
                Log::info($ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine());
                DB::rollBack();
                return json_encode([
                    'status' => -1,
                    'message' => $ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine()
                ]);
            }

            return json_encode([
                'status' => '1',
                'message' => '资料添加成功',
                'data' => [
                    'store_id' => $store_id
                ]
            ]);
        } catch (\Exception $ex) {
            Log::info($ex);
            $this->status = -1;
            $this->message = $ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine();
            return $this->format();
        }
    }


    //城市名称
    public function city_name($city_code)
    {
        $city_name = "";
        $data = ProvinceCity::where('area_code', $city_code)->first();
        if ($data) {
            $city_name = $data->area_name;
        }

        return $city_name;

    }


    //所有系统通道
    public function pay_ways_all(Request $request)
    {
        try {
            $user = $this->parseToken();
            $store_id = $request->get('store_id');

            $store = Store::where('store_id', $store_id)
                ->select('user_id')
                ->first();
            if (!$store) {
                return json_encode([
                    'status' => 2,
                    'message' => '门店不存在',
                    'data' => []
                ]);
            }

            $data = [];
            $store_ways_desc = DB::table('store_ways_desc')
                ->select()
                ->orderBy('company')
                ->get();
                // echo '<pre>';
                // print_r($store_ways_desc);
                // die;

            $store_ways_desc = json_decode(json_encode($store_ways_desc), true);
            //
            $companys=array_column($store_ways_desc,"company");
            $UserRateArray = UserRate::query()->whereIn('company', $companys)
                ->where('user_id', $store->user_id)
                ->select(["id","company"])
                ->get()->toArray();
            $UserRateArray=array_combine(array_column($UserRateArray,"company"),$UserRateArray);

            $ways_type_array=array_column($store_ways_desc,"ways_type");
            $hasArray = StorePayWay::query()->where('store_id', $store_id)
                ->whereIn('ways_type', $ways_type_array)
                ->get()->toArray();
            $hasArray=array_combine(array_column($hasArray,"ways_type"),$hasArray);

            //代理商费用
            $user_rate_array = UserRate::query()->where('user_id', $store->user_id)
                ->whereIn('ways_type', $ways_type_array)
                ->get()->toArray();
            $user_rate_array=array_combine(array_column($user_rate_array,"ways_type"),$user_rate_array);

            foreach ($store_ways_desc as $k => $value) {
                //代理不开通 不显示
//                $UserRate = UserRate::where('company', $value['company'])
//                    ->where('user_id', $store->user_id)
//                    ->select('id')
//                    ->first();
                $UserRate=$UserRateArray[$value['company']]??[];
                if (!$UserRate) {
                    continue;
                }
                $value["type"]=0;
                //1 支付宝  2 微信 3 银联云闪付
                if($value["ways_source"]=="alipay"){
                    $value["type"]=1;
                }else if($value["ways_source"]=="weixin"){
                    $value["type"]=2;
                }else if (in_array($value["ways_source"], ["unionpay", "Bestpay"])) {
                    $value["type"]=3;
                }
//                $has = StorePayWay::where('store_id', $store_id)
//                    ->where('ways_type', $value['ways_type'])
//                    ->first();
                $has=$hasArray[$value['ways_type']]??[];
                if ($has) {
                    $data[$k] = $value;
                    $data[$k]['rate'] = $has["rate"];
                    $data[$k]['status'] = $has["status"];
                    $data[$k]['status_desc'] = $has["status_desc"];
                    $data[$k]['icon'] = '';
                    $data[$k]['rate_a'] = $has["rate_a"];
                    $data[$k]['rate_b'] = $has["rate_b"];
                    $data[$k]['rate_b_top'] = $has["rate_b_top"];
                    $data[$k]['rate_c'] = $has["rate_c"];
                    $data[$k]['rate_d'] = $has["rate_d"];
                    $data[$k]['rate_d_top'] = $has["rate_d_top"];
                    $data[$k]['rate_e'] = $has["rate_e"];
                    $data[$k]['rate_f'] = $has["rate_f"];
                    $data[$k]['rate_f_top'] = $has["rate_f_top"];
                    //如果是刷卡费率读取
                    //新大陆刷卡
//                    if (in_array($value['ways_type'], [8005, 6005])) {
//                        $data[$k]['rate'] = '贷记卡=' . $has->rate_e . '%,借记卡=' . $has->rate_f;
//                    }
//
//                    //银联扫码
//                    if (in_array($value['ways_type'], [8004, 6004])) {
//                        $data[$k]['rate'] = $has->rate_c;
//                    }
//                    $data = array_values($data);

                } else {
                    $rate = $value['store_all_rate'];//默认是系统费率
                    $rate_a = $value['store_all_rate_a'];//默认是系统费率
                    $rate_b = $value['store_all_rate_b'];//默认是系统费率
                    $rate_b_top = $value['store_all_rate_b_top'];//默认是系统费率
                    $rate_e = $value['store_all_rate_e'];//默认是系统费率
                    $rate_c = $value['store_all_rate_c'];//默认是系统费率
                    $rate_d = $value['store_all_rate_d'];//默认是系统费率
                    $rate_d_top = $value['store_all_rate_d_top'];//默认是系统费率
                    $rate_f = $value['store_all_rate_f'];//默认是系统费率
                    $rate_f_top = $value['store_all_rate_f_top'];//默认是系统费率
                    $data[$k] = $value;
                    //代理商的费率
//                    $user_rate = UserRate::where('user_id', $store->user_id)
//                        ->where('ways_type', $value['ways_type'])
//                        ->first();
                    $user_rate=$user_rate_array[$value['ways_type']]??[];

                    if ($user_rate) {
                        $rate = $user_rate["store_all_rate"];//显示代理商的默认费率
                        $rate_a = $user_rate["store_all_rate_a"];//默认是系统费率
                        $rate_b = $user_rate["store_all_rate_b"];//默认是系统费率
                        $rate_c = $user_rate["store_all_rate_c"];//默认是系统费率
                        $rate_b_top = $user_rate["store_all_rate_b_top"];//默认是系统费率
                        $rate_d = $user_rate["store_all_rate_d"];//默认是系统费率
                        $rate_d_top = $user_rate["store_all_rate_d_top"];//默认是系统费率
                        $rate_e = $user_rate["store_all_rate_e"];//默认是系统费率
                        $rate_f = $user_rate["store_all_rate_f"];//默认是系统费率
                        $rate_f_top = $user_rate["store_all_rate_f_top"];//默认是系统费率
                    }

                    $data[$k]['rate'] = $rate; //
                    $data[$k]['status'] = 0;
                    $data[$k]['status_desc'] = '未开通';
                    $data[$k]['icon'] = '';
                    $data[$k]['rate_a'] = $rate_a;
                    $data[$k]['rate_b'] = $rate_b;
                    $data[$k]['rate_c'] = $rate_c;
                    $data[$k]['rate_d'] = $rate_d;
                    $data[$k]['rate_d_top'] = $rate_d_top;
                    $data[$k]['rate_b_top'] = $rate_b_top;
                    $data[$k]['rate_e'] = $rate_e;
                    $data[$k]['rate_f'] = $rate_f;
                    $data[$k]['rate_f_top'] = $rate_f_top;
                    //如果是刷卡费率读取
                    //新大陆刷卡
//                    if (in_array($value['ways_type'], [8005, 6005])) {
//                        $data[$k]['rate'] = '贷记卡=' . $rate_e . '%,借记卡=' . $rate_f;
//                    }
//
//                    //银联扫码
//                    if (in_array($value['ways_type'], [8004, 6004])) {
//                        $data[$k]['rate'] = $rate_c;
//                    }

                }

            }

            return json_encode(['status' => 1, 'data' => $data]);
        } catch (\Exception $exception) {
            return json_encode(['status' => 0, 'message' => $exception->getMessage() . ' - ' . $exception->getLine()]);
        }
    }


    //所有通道信息
    public function pay_ways_all_new()
    {
        try {
            $token = $this->parseToken();

            $data = DB::table('store_ways_desc')
                ->select('id', 'ways_type', 'ways_desc', 'ways_source', 'company')
                ->get();

        } catch (\Exception $exception) {
            $this->status = '2';
            $this->message = $exception->getMessage() . ' - ' . $exception->getLine();
            return $this->format();
        }

        $this->status = '1';
        $this->message = '成功返回数据';
        return $this->format($data);
    }


    //所有系统通道-new
    public function store_all_pay_way_lists(Request $request)
    {
        try {
            $user = $this->parseToken();
            $store_id = $request->get('store_id');

            $data = [];
            $store_all_pay_way_lists = DB::table('store_all_pay_way_lists')
                ->select('ways_count', 'company', 'company_desc')
                ->get();
            $store_all_pay_way_lists = json_decode(json_encode($store_all_pay_way_lists), true);
            $i = 0;
            foreach ($store_all_pay_way_lists as $k => $value) {

                $has = StorePayWay::where('store_id', $store_id)
                    ->where('company', $value['company'])
                    ->select('status')
                    ->first();

                //代理不开通不
                $UserRate = UserRate::where('company', $value['company'])
                    ->where('user_id', $user->user_id)
                    ->select('id')
                    ->first();
                if (!$UserRate) {
                    continue;
                }


                $data[$i] = $value;
                $data[$i]['status'] = '0';
                $data[$i]['status_desc'] = '未开通';
                //
                if ($has && $has->status == 1) {
                    $data[$i]['status'] = '1';
                    $data[$i]['status_desc'] = '开通成功';
                }

                if ($has && $has->status == 2) {
                    $data[$i]['status'] = '2';
                    $data[$i]['status_desc'] = '审核中';
                }

                if ($has && $has->status == 3) {
                    $data[$i]['status'] = '3';
                    $data[$i]['status_desc'] = '开通失败';
                }

                $i = $i + 1;

            }
            $edit = array_column($data, 'status');
            array_multisort($edit, SORT_DESC, $data);
            return json_encode(['status' => 1, 'data' => $data]);
        } catch (\Exception $exception) {
            return json_encode(['status' => 0, 'message' => $exception->getMessage() . ' - ' . $exception->getLine()]);
        }
    }


    //所有系统通道已经开通的-new
    public function store_open_pay_way_lists(Request $request)
    {
        try {
            $user = $this->parseToken();
            $store_id = $request->get('store_id', '');

            if ($store_id) {
                $data = DB::table('store_all_pay_way_lists')
                    ->join('store_pay_ways', 'store_all_pay_way_lists.company', '=', 'store_pay_ways.company')
                    ->where('store_pay_ways.store_id', $store_id)
                    ->where('store_pay_ways.status', 1)
                    ->select('store_all_pay_way_lists.company', 'store_all_pay_way_lists.company_desc')
                    ->distinct()
                    ->get();
            } else {
                $data = DB::table('store_all_pay_way_lists')
                    ->get();
            }


            return json_encode(['status' => 1, 'data' => $data]);


        } catch (\Exception $exception) {
            return json_encode(['status' => 0, 'message' => $exception->getMessage() . ' - ' . $exception->getLine()]);
        }
    }


    //单个通道信息查询-new
    public function company_pay_ways_info(Request $request)
    {
        try {
            $user = $this->parseToken();
            $store_id = $request->get('store_id');
            $company = $request->get('company');
            $data = [];
            $store_ways_desc = DB::table('store_ways_desc')
                ->where('company', $company)->get();


            $store = Store::where('store_id', $store_id)
                ->select('user_id')
                ->first();

            $ways_desc = "";
            $ways_status = "";
            foreach ($store_ways_desc as $k => $v) {
                $data[$k]['ways_desc'] = $v->ways_desc;
                $data[$k]['ways_source'] = $v->ways_source;
                $data[$k]['settlement_type'] = $v->settlement_type;
                $data[$k]['ways_type'] = $v->ways_type;

                $has = StorePayWay::where('store_id', $store_id)
                    ->where('ways_type', $v->ways_type)
                    ->first();

                $ways_desc = $ways_desc . $data[$k]['ways_desc'] . ',';


                if ($has) {
                    $data[$k]['rate'] = $has->rate;
                    $data[$k]['status'] = $has->status;
                    $data[$k]['status_desc'] = $has->status_desc;
                    $data[$k]['status_content'] = $has->status_desc;;

                } else {

                    $rate = $v->rate;//默认是系统费率
                    //代理商的费率
                    $user_rate = UserRate::where('user_id', $store->user_id)
                        ->where('ways_type', $v->ways_type)
                        ->select('store_all_rate')
                        ->first();
                    if ($user_rate) {
                        $rate = $user_rate->store_all_rate;
                    }
                    $data[$k]['rate'] = $rate; //
                    $data[$k]['status'] = 0;
                    $data[$k]['status_desc'] = '未开通';
                    $data[$k]['status_content'] = '未开通';

                }

                $ways_status = $data[$k]['status'];
            }


            return json_encode(['status' => 1, 'message' => '数据返回成功', 'ways_status' => $ways_status, 'ways_desc' => $ways_desc, 'data' => $data]);


        } catch (\Exception $exception) {
            return json_encode(['status' => 0, 'message' => $exception->getMessage() . ' - ' . $exception->getLine()]);
        }
    }


    //申请通道-new
    public function open_company_pay_ways(Request $request)
    {
        try {

            $user = $this->parseToken();
            $company = $request->get('company');
            $store_id = $request->get('store_id');
            $code = $request->get('code', '888888');
            $SettleModeType = $request->get('Settle_mode_type', '01');//结算方式
            $sp = new StorePayWaysController();

            $store_ways_desc = DB::table('store_ways_desc')
                ->where('company', $company)
                ->first();
            $type = $store_ways_desc->ways_type;

            return $sp->base_open_ways($type, $code, $store_id, $SettleModeType, '', '');
        } catch (\Exception $ex) {
            return json_encode([
                'status' => '2',
                'message' => $ex->getMessage() .' | '.$ex->getFile().' | '. $ex->getLine(),
            ]);
        }
    }

    //申请通道-小程序
    public function open_company_pay_ways1(Request $request){
        try {
            $user = $this->parseToken();
            $company = $request->get('company');
            $store_id = $request->get('store_id');
            $code = $request->get('code', '888888');
            $SettleModeType = $request->get('Settle_mode_type', '01');//结算方式
            $sp = new StorePayWaysController();

            $store_ways_desc = DB::table('store_ways_desc')
                ->where('company', $company)
                ->first();
            $type = $store_ways_desc->ways_type;

            return $sp->base_open_ways1($type, $code, $store_id, $SettleModeType, '', '');
        } catch (\Exception $ex) {
            return json_encode([
                'status' => '2',
                'message' => $ex->getMessage() .' | '.$ex->getFile().' | '. $ex->getLine(),
            ]);
        }
    }


    //查询单个通道详细
    public function pay_ways_info(Request $request)
    {
        try {
            $user = $this->parseToken();
            $store_id = $request->get('store_id');
            $ways_type = $request->get('ways_type');

            $store = Store::where('store_id', $store_id)
                ->select('user_id')
                ->first();

            if (!$store) {
                return json_encode(['status' => 2, 'message' => '门店不存在', 'data' => []]);
            }
            $data = [];
            $store_ways_desc = DB::table('store_ways_desc')->where('ways_type', $ways_type)->first();
            $data['ways_desc'] = $store_ways_desc->ways_desc;
            $data['ways_source'] = $store_ways_desc->ways_source;
            $data['settlement_type'] = $store_ways_desc->settlement_type;
            $data['ways_type'] = $store_ways_desc->ways_type;

            $has = StorePayWay::where('store_id', $store_id)->where('ways_type', $ways_type)->first();
            if ($has) {
                $data['rate'] = $has->rate;
                $data['status'] = $has->status;
                $data['status_desc'] = $has->status_desc;

            } else {
                $rate = $store_ways_desc->rate;//默认是系统费率
                //代理商的费率
                $user_rate = UserRate::where('user_id', $store->user_id)
                    ->where('ways_type', $ways_type)
                    ->select('rate')
                    ->first();
                if ($user_rate) {
                    $rate = $user_rate->rate;
                }
                $data['rate'] = $rate; //
                $data['status'] = 0;
                $data['status_desc'] = '未开通';
            }


            return json_encode(['status' => 1, 'data' => $data]);


        } catch (\Exception $exception) {
            return json_encode(['status' => -1, 'message' => $exception->getMessage() . ' - ' . $exception->getLine()]);
        }
    }


    //门店设置费率-扫码
    public function edit_store_rate(Request $request){
        try {
            $user = $this->parseToken();
            $store_id = $request->get('store_id');
            $ways_type = $request->get('ways_type');
            $rate = $request->get('rate');



            // $pash = $request->path();
            // $pass = $request->get('pass', '');
            // // print_r($user);die;
            // $pass = substr(base64_decode($pass), 0, -5);
            // $pass_verify = $this->pass_verify($pass, $pash); // 密钥验证

            // $pass_verify = json_decode($pass_verify, true);
            // // print_r( $pass_verify );die;
            // if( $pass_verify['status'] == '1'){
            //     $pass_id = DB::table('pass')->where(['pass' => $pass])->value('id'); // 密钥id
            //     $remark = '门店修改费率: 门店id: ' . $store_id . '修改费率为: ' . $rate;
            //     $this->operation_log($this->parseToken()->user_id, 3, $remark , $pass_id);
            // }else{
            //     return json_encode(['status' => 2, 'message' => '验证不通过', 'data' => []]);
            // }



            $store = Store::where('store_id', $store_id)
                ->first();
            if (!$store) {
                return json_encode(['status' => 2, 'message' => '门店不存在', 'data' => []]);
            }

            if ($rate > 30) {
                return json_encode(['status' => 2, 'message' => '设置超限', 'data' => []]);
            }

            //共享通道不支持修改费率
            if ($user->level > 0 && $store->pay_ways_type) {
                $store_pid = Store::where('id', $store->pid)
                    ->select('store_id')
                    ->first();
                if ($store_pid) {
                    $StorePayWay = StorePayWay::where('store_id', $store_pid->store_id)
                        ->where('ways_type', $ways_type)
                        ->select('rate')
                        ->first();
                    if ($StorePayWay) {
                        if ($rate !== $StorePayWay->rate) {
                            return json_encode(['status' => 2, 'message' => '共享通道不支持修改费率']);
                        }
                    }
                }
            }

            $store_pid = $store->pid;
            $config_id = $store->config_id;
            //代理商的费率
            $user_rate = UserRate::where('user_id', $store->user_id)
                ->where('ways_type', $ways_type)
                ->select('rate')
                ->first();
            if (!$user_rate) {
                return json_encode(['status' => 2, 'message' => '代理商未设置费率', 'data' => []]);
            }

            //不能大于代理商的成本
            if ($user->level > 0 && $rate < $user_rate->rate) {
                return json_encode(['status' => 2, 'message' => '费率不能低于代理商的费率', 'data' => []]);
            }

            $all_pay_ways = DB::table('store_ways_desc')
                ->where('ways_type', $ways_type)
                ->select('company')
                ->first();
            $company = $all_pay_ways->company;

            $has = StorePayWay::where('store_id', $store_id)->where('ways_type', $ways_type)
                ->first();
            if ($has) {
                //修改费率
                $user = User::where('id', $user->user_id)->first();
                $hasPermission = $user->hasPermissionTo('修改费率-' . $company);
                if (!$hasPermission) {
                    return json_encode(['status' => 2, 'message' => '没有权限设置费率']);
                }

                return json_encode(['status' => 2, 'message' => '暂无权限修改，请联系中台']);

                $status = $has->status;
                $status_desc = $has->status_desc;

                //如果通道审核中不允许修改费率
                if ($status == 2) {
                    return json_encode(['status' => 2, 'message' => '通道审核中不允许修改费率', 'data' => []]);
                }

                //京东收银通道修改费率
                if ($user->level > 0 && $has->status == 1 && 5999 < $ways_type && $ways_type < 6999) {
                    return json_encode(['status' => 2, 'message' => '京东通道开通后不支持软件修改', 'data' => $request->except(['token'])]);
                }

                //快钱支付同步平台费率
                if ($has->status == 1 && 2999 < $ways_type && $ways_type < 3999) {
                    $Merchant = MyBankStore::where('OutMerchantId', $store_id)
                        ->select('MerchantId')
                        ->first();

                    if (!$Merchant) {
                        return json_encode(['status' => 2, 'message' => '快钱商户信息不存在']);
                    }

                    //暂时全部修改
                    $data = [
                        'ta_rate' => $rate + 0.02,
                        'tb_rate' => $rate,
                        'MerchantId' => $Merchant->MerchantId,
                    ];
                    $obj = new MyBankController();
                    $re = $obj->StoreRate($data);

                    if ($re['status'] == 2) {
                        return json_encode($re);
                    }
                }

                //新大陆同步平台费率
                if ($user->level > 0 && $has->status == 1 && 7999 < $ways_type && $ways_type < 8999) {


                    $OBJ = new  UpdateController();

                    //修改费率
                    $up_data = [
                        'store_id' => $store_id,
                        'email' => $store->people_phone . '@139.com',
                        'phone' => $store->people_phone,
                        'rate' => $rate,
                        'rate_a' => $has->rate_a,
                        'rate_c' => $has->rate_c,
                        'rate_f' => $has->rate_f,
                        'rate_f_top' => $has->rate_f_top,
                        'rate_e' => $has->rate_e,
                    ];

                    // $return = $OBJ->update_store_rate($up_data);
                    //  return $return;

                    return json_encode(['status' => 2, 'message' => '暂不支持修改', 'data' => $request->except(['token'])]);
                }

                //传化银行同步平台费率
                if ($has->status == 1 && 11999 < $ways_type && $ways_type < 12999) {
                    $config = new TfConfigController();
                    $h_merchant = $config->tf_merchant($store_id, $store_pid);
                    if (!$h_merchant) {
                        return json_encode([
                            'status' => 2,
                            'message' => '传化商户号不存在'
                        ]);
                    }

                    $h_config = $config->tf_config($config_id, $h_merchant->qd);
                    if (!$h_config) {
                        return json_encode([
                            'status' => 2,
                            'message' => '传化配置不存在请检查配置'
                        ]);
                    }

                    $sub_mch_id = $h_merchant->sub_mch_id;
                    $rate1 = $rate * 10;
                    $post_data = [
                        'sub_mch_id' => $sub_mch_id,
                        'WECHAT_POS' => $rate1,
                        'WECHAT_SCAN' => $rate1,
                        'WECHAT_MP' => $rate1,
                        'ALIPAY_POS' => $rate1,
                        'ALIPAY_SCAN' => $rate1,
                        'ALIPAY_MP' => $rate1,
                    ];

                    $method = '/openapi/merchant/rate';
                    $obj = new \App\Api\Controllers\Tfpay\BaseController();
                    $obj->mch_id = $h_config->mch_id;
                    $obj->pub_key = $h_config->pub_key;
                    $obj->pri_key = $h_config->pri_key;
                    $re = $obj->api($post_data, $method, false);
                    if ($re['code'] != 0) {
                        return json_encode([
                            'status' => 2,
                            'message' => $re['msg']
                        ]);
                    }
                }

                //随行付银行同步平台费率
                // if ($has->status == 1 && 12999 < $ways_type && $ways_type < 13999) {
                //     $config = new VbillConfigController();

                //     $vbill_config = $config->vbill_config($config_id);
                //     if (!$vbill_config) {
                //         return json_encode([
                //             'status' => 2,
                //             'message' => '随行付配置不存在请检查配置'
                //         ]);
                //     }
                //     $vbill_merchant = $config->vbill_merchant($store_id, $store_pid);
                //     if (!$vbill_merchant) {
                //         return json_encode([
                //             'status' => 2,
                //             'message' => '随行付商户号不存在'
                //         ]);
                //     }

                //     $obj = new \App\Api\Controllers\Vbill\BaseController();
                //     $data = [
                //         'orgId' => $vbill_config->orgId,
                //         'reqId' => time(),
                //         'version' => '1.0',
                //         'timestamp' => date('Ymdhis', time()),
                //         'signType' => 'RSA',
                //         'reqData' => [
                //             'mno' => $vbill_merchant->mno, //商户编号
                //             'qrcodeList' => [
                //                 [
                //                     'rateType' => '01', //微信
                //                     'rate' => $rate,
                //                 ],
                //                 [
                //                     'rateType' => '02', //支付宝
                //                     'rate' => $rate,
                //                 ],
                //                 [
                //                     'rateType' => '06', //银联单笔小于等于1000
                //                     'rate' => '0.6',
                //                 ],
                //                 [
                //                     'rateType' => '07', //银联单笔大于1000
                //                     'rate' => '0.6',
                //                 ]
                //             ]
                //         ],
                //     ];
                //     $url = $obj->update_rate;
                //     $re = $obj->execute($data, $url, $vbill_config->privateKey, $vbill_config->sxfpublic);
                //     //系统错误
                //     if ($re['data']['respData']['bizCode'] != '0000') {
                //         return json_encode(['status' => 2, 'message' => $re['data']['respData'] ['bizMsg']]);

                //     }
                // }

                //随行付a银行同步平台费率
                if ($has->status == 1 && 18999 < $ways_type && $ways_type < 19999) {
                    $config = new VbillConfigController();
                    $vbill_config = $config->vbilla_config($config_id);
                    if (!$vbill_config) {
                        return json_encode([
                            'status' => 2,
                            'message' => '随行付配置不存在请检查配置'
                        ]);
                    }
                    $vbill_merchant = $config->vbilla_merchant($store_id, $store_pid);
                    if (!$vbill_merchant) {
                        return json_encode([
                            'status' => 2,
                            'message' => '随行付商户号不存在'
                        ]);
                    }

                    $obj = new \App\Api\Controllers\Vbill\BaseController();
                    $data = [
                        'orgId' => $vbill_config->orgId,
                        'reqId' => time(),
                        'version' => '1.0',
                        'timestamp' => date('Ymdhis', time()),
                        'signType' => 'RSA',
                        'reqData' => [
                            'mno' => $vbill_merchant->mno,//商户编号
                            'qrcodeList' => [
                                [
                                    'rateType' => '01',
                                    'rate' => $rate,
                                ],
                                [
                                    'rateType' => '02',
                                    'rate' => $rate,
                                ],
                                [
                                    'rateType' => '06',
                                    'rate' => '0.6',
                                ],
                                [
                                    'rateType' => '07',
                                    'rate' => '0.6',
                                ]
                            ]
                        ],
                    ];
                    $url = $obj->update_rate;
                    $re = $obj->execute($data, $url, $vbill_config->privateKey, $vbill_config->sxfpublic);
                    //系统错误
                    if ($re['data']['respData']['bizCode'] != '0000') {
                        return json_encode(['status' => 2, 'message' => $re['data']['respData'] ['bizMsg']]);

                    }
                }

		        //葫芦天下
                if ($has->status == 1 && 22999 < $ways_type && $ways_type < 23999) {
                    $HltxStore = HltxStore::where('store_id', $store_id)->first();
                    if ($HltxStore && !empty($HltxStore->mer_no)) {
                        //允许修改费率
                        $obj = new ManageController();
                        $hltx_config = $obj->pay_config($config_id);
                        $obj->init($hltx_config);//初始化配置
                        $return = $obj->hltx_edit_payinfo_merchant($HltxStore->mer_no, $rate);
                        if ($return['status'] != 1) {
                            return json_encode(['status' => 2, 'message' => 'HL费率修改失败:' . $return['message'], 'data' => []]);
                        }
                        $return = $return['data'];
                        if ($return['status'] == '00') {
                            //成功
                        } else {
                            return json_encode(['status' => 2, 'message' => '请求申请修改用户HL通道费率业务失败', 'data' => []]);
                        }
                    }
                }


            } else {
                //添加
                $user = User::where('id', $user->user_id)->first();
                $hasPermission = $user->hasPermissionTo('设置费率-' . $company);
                if (!$hasPermission) {
                    return json_encode(['status' => 2, 'message' => '没有权限设置费率']);
                }


                $status = 0;
                $status_desc = '未开通';
            }

            //查找下级门店共享通道
            if ($store->pid == 0) {
                $sub_store = Store::where('pid', $store->id)
                    ->where('pay_ways_type', 1)
                    ->select('store_id')
                    ->get();

                foreach ($sub_store as $k => $v) {
                    //暂时全部修改
                    $data['store_id'] = $v->store_id;
                    $data['rate'] = $rate;
                    $data['status'] = $status;
                    $data['status_desc'] = $status_desc;
                    $data['company'] = $company;
                    $this->send_ways_data($data);
                }
            }

            //暂时全部修改
            $data['store_id'] = $store_id;
            $data['rate'] = $rate;
            $data['status'] = $status;
            $data['status_desc'] = $status_desc;
            $data['company'] = $company;
            $return = $this->send_ways_data($data);
            return $return;
        } catch (\Exception $exception) {
            return json_encode(['status' => -1, 'message' => $exception->getMessage() . ' - ' . $exception->getLine()]);
        }

    }

    //门店设置费率-扫码
    public function edit_store_rate_pc(Request $request){
        try {
            $user = $this->parseToken();
            $store_id = $request->get('store_id');
            $ways_type = $request->get('ways_type');
            $rate = $request->get('rate');

            $pash = $request->path();
            $pass = $request->get('pass', '');
            $pass = substr(base64_decode($pass), 0, -5);
            $pass_verify = $this->pass_verify($pass, $pash); // 密钥验证

            $pass_verify = json_decode($pass_verify, true);
            if( $pass_verify['status'] == '1'){
                $pass_id = DB::table('pass')->where(['pass' => $pass])->value('id'); // 密钥id
                $remark = '门店修改费率: 门店id: ' . $store_id . '修改费率为: ' . $rate;
                $this->operation_log($this->parseToken()->user_id, 3, $remark , $pass_id);
            }else{
                return json_encode(['status' => 2, 'message' => '验证不通过', 'data' => []]);
            }



            $store = Store::where('store_id', $store_id)
                ->first();
            if (!$store) {
                return json_encode(['status' => 2, 'message' => '门店不存在', 'data' => []]);
            }

            if ($rate > 30) {
                return json_encode(['status' => 2, 'message' => '设置超限', 'data' => []]);
            }

            //共享通道不支持修改费率
            if ($user->level > 0 && $store->pay_ways_type) {
                $store_pid = Store::where('id', $store->pid)
                    ->select('store_id')
                    ->first();
                if ($store_pid) {
                    $StorePayWay = StorePayWay::where('store_id', $store_pid->store_id)
                        ->where('ways_type', $ways_type)
                        ->select('rate')
                        ->first();
                    if ($StorePayWay) {
                        if ($rate !== $StorePayWay->rate) {
                            return json_encode(['status' => 2, 'message' => '共享通道不支持修改费率']);
                        }
                    }
                }
            }

            $store_pid = $store->pid;
            $config_id = $store->config_id;
            //代理商的费率
            $user_rate = UserRate::where('user_id', $store->user_id)
                ->where('ways_type', $ways_type)
                ->select('rate')
                ->first();
            if (!$user_rate) {
                return json_encode(['status' => 2, 'message' => '代理商未设置费率', 'data' => []]);
            }

            //不能大于代理商的成本
            if ($user->level > 0 && $rate < $user_rate->rate) {
                return json_encode(['status' => 2, 'message' => '费率不能低于代理商的费率', 'data' => []]);
            }

            $all_pay_ways = DB::table('store_ways_desc')
                ->where('ways_type', $ways_type)
                ->select('company')
                ->first();
            $company = $all_pay_ways->company;

            $has = StorePayWay::where('store_id', $store_id)->where('ways_type', $ways_type)
                ->first();
            if ($has) {
                //修改费率
                $user = User::where('id', $user->user_id)->first();
                $hasPermission = $user->hasPermissionTo('修改费率-' . $company);
                if (!$hasPermission) {
                    return json_encode(['status' => 2, 'message' => '没有权限设置费率']);
                }

                $status = $has->status;
                $status_desc = $has->status_desc;

                //如果通道审核中不允许修改费率
                if ($status == 2) {
                    return json_encode(['status' => 2, 'message' => '通道审核中不允许修改费率', 'data' => []]);
                }

                //京东收银通道修改费率
                if ($user->level > 0 && $has->status == 1 && 5999 < $ways_type && $ways_type < 6999) {
                    return json_encode(['status' => 2, 'message' => '京东通道开通后不支持软件修改', 'data' => $request->except(['token'])]);
                }

                //快钱支付同步平台费率
                if ($has->status == 1 && 2999 < $ways_type && $ways_type < 3999) {
                    $Merchant = MyBankStore::where('OutMerchantId', $store_id)
                        ->select('MerchantId')
                        ->first();

                    if (!$Merchant) {
                        return json_encode(['status' => 2, 'message' => '快钱商户信息不存在']);
                    }

                    //暂时全部修改
                    $data = [
                        'ta_rate' => $rate + 0.02,
                        'tb_rate' => $rate,
                        'MerchantId' => $Merchant->MerchantId,
                    ];
                    $obj = new MyBankController();
                    $re = $obj->StoreRate($data);

                    if ($re['status'] == 2) {
                        return json_encode($re);
                    }
                }

                //新大陆同步平台费率
                if ($user->level > 0 && $has->status == 1 && 7999 < $ways_type && $ways_type < 8999) {


                    $OBJ = new  UpdateController();

                    //修改费率
                    $up_data = [
                        'store_id' => $store_id,
                        'email' => $store->people_phone . '@139.com',
                        'phone' => $store->people_phone,
                        'rate' => $rate,
                        'rate_a' => $has->rate_a,
                        'rate_c' => $has->rate_c,
                        'rate_f' => $has->rate_f,
                        'rate_f_top' => $has->rate_f_top,
                        'rate_e' => $has->rate_e,
                    ];

                    // $return = $OBJ->update_store_rate($up_data);
                    //  return $return;

                    return json_encode(['status' => 2, 'message' => '暂不支持修改', 'data' => $request->except(['token'])]);
                }

                //传化银行同步平台费率
                if ($has->status == 1 && 11999 < $ways_type && $ways_type < 12999) {
                    $config = new TfConfigController();
                    $h_merchant = $config->tf_merchant($store_id, $store_pid);
                    if (!$h_merchant) {
                        return json_encode([
                            'status' => 2,
                            'message' => '传化商户号不存在'
                        ]);
                    }

                    $h_config = $config->tf_config($config_id, $h_merchant->qd);
                    if (!$h_config) {
                        return json_encode([
                            'status' => 2,
                            'message' => '传化配置不存在请检查配置'
                        ]);
                    }

                    $sub_mch_id = $h_merchant->sub_mch_id;
                    $rate1 = $rate * 10;
                    $post_data = [
                        'sub_mch_id' => $sub_mch_id,
                        'WECHAT_POS' => $rate1,
                        'WECHAT_SCAN' => $rate1,
                        'WECHAT_MP' => $rate1,
                        'ALIPAY_POS' => $rate1,
                        'ALIPAY_SCAN' => $rate1,
                        'ALIPAY_MP' => $rate1,
                    ];

                    $method = '/openapi/merchant/rate';
                    $obj = new \App\Api\Controllers\Tfpay\BaseController();
                    $obj->mch_id = $h_config->mch_id;
                    $obj->pub_key = $h_config->pub_key;
                    $obj->pri_key = $h_config->pri_key;
                    $re = $obj->api($post_data, $method, false);
                    if ($re['code'] != 0) {
                        return json_encode([
                            'status' => 2,
                            'message' => $re['msg']
                        ]);
                    }
                }

                //随行付银行同步平台费率
                if ($has->status == 1 && 12999 < $ways_type && $ways_type < 13999) {
                    $config = new VbillConfigController();

                    $vbill_config = $config->vbill_config($config_id);
                    if (!$vbill_config) {
                        return json_encode([
                            'status' => 2,
                            'message' => '随行付配置不存在请检查配置'
                        ]);
                    }
                    $vbill_merchant = $config->vbill_merchant($store_id, $store_pid);
                    if (!$vbill_merchant) {
                        return json_encode([
                            'status' => 2,
                            'message' => '随行付商户号不存在'
                        ]);
                    }

                    $obj = new \App\Api\Controllers\Vbill\BaseController();
                    $data = [
                        'orgId' => $vbill_config->orgId,
                        'reqId' => time(),
                        'version' => '1.0',
                        'timestamp' => date('Ymdhis', time()),
                        'signType' => 'RSA',
                        'reqData' => [
                            'mno' => $vbill_merchant->mno, //商户编号
                            'qrcodeList' => [
                                [
                                    'rateType' => '01', //微信
                                    'rate' => $rate,
                                ],
                                [
                                    'rateType' => '02', //支付宝
                                    'rate' => $rate,
                                ],
                                [
                                    'rateType' => '06', //银联单笔小于等于1000
                                    'rate' => '0.6',
                                ],
                                [
                                    'rateType' => '07', //银联单笔大于1000
                                    'rate' => '0.6',
                                ]
                            ]
                        ],
                    ];
                    $url = $obj->update_rate;
                    $re = $obj->execute($data, $url, $vbill_config->privateKey, $vbill_config->sxfpublic);
                    //系统错误
                    if ($re['data']['respData']['bizCode'] != '0000') {
                        return json_encode(['status' => 2, 'message' => $re['data']['respData'] ['bizMsg']]);

                    }
                }

                //随行付a银行同步平台费率
                if ($has->status == 1 && 18999 < $ways_type && $ways_type < 19999) {
                    $config = new VbillConfigController();
                    $vbill_config = $config->vbilla_config($config_id);
                    if (!$vbill_config) {
                        return json_encode([
                            'status' => 2,
                            'message' => '随行付配置不存在请检查配置'
                        ]);
                    }
                    $vbill_merchant = $config->vbilla_merchant($store_id, $store_pid);
                    if (!$vbill_merchant) {
                        return json_encode([
                            'status' => 2,
                            'message' => '随行付商户号不存在'
                        ]);
                    }

                    $obj = new \App\Api\Controllers\Vbill\BaseController();
                    $data = [
                        'orgId' => $vbill_config->orgId,
                        'reqId' => time(),
                        'version' => '1.0',
                        'timestamp' => date('Ymdhis', time()),
                        'signType' => 'RSA',
                        'reqData' => [
                            'mno' => $vbill_merchant->mno,//商户编号
                            'qrcodeList' => [
                                [
                                    'rateType' => '01',
                                    'rate' => $rate,
                                ],
                                [
                                    'rateType' => '02',
                                    'rate' => $rate,
                                ],
                                [
                                    'rateType' => '06',
                                    'rate' => '0.6',
                                ],
                                [
                                    'rateType' => '07',
                                    'rate' => '0.6',
                                ]
                            ]
                        ],
                    ];
                    $url = $obj->update_rate;
                    $re = $obj->execute($data, $url, $vbill_config->privateKey, $vbill_config->sxfpublic);
                    //系统错误
                    if ($re['data']['respData']['bizCode'] != '0000') {
                        return json_encode(['status' => 2, 'message' => $re['data']['respData'] ['bizMsg']]);

                    }
                }

		        //葫芦天下
                if ($has->status == 1 && 22999 < $ways_type && $ways_type < 23999) {
                    $HltxStore = HltxStore::where('store_id', $store_id)->first();
                    if ($HltxStore && !empty($HltxStore->mer_no)) {
                        //允许修改费率
                        $obj = new ManageController();
                        $hltx_config = $obj->pay_config($config_id);
                        $obj->init($hltx_config);//初始化配置
                        $return = $obj->hltx_edit_payinfo_merchant($HltxStore->mer_no, $rate);
                        if ($return['status'] != 1) {
                            return json_encode(['status' => 2, 'message' => 'HL费率修改失败:' . $return['message'], 'data' => []]);
                        }
                        $return = $return['data'];
                        if ($return['status'] == '00') {
                            //成功
                        } else {
                            return json_encode(['status' => 2, 'message' => '请求申请修改用户HL通道费率业务失败', 'data' => []]);
                        }
                    }
                }


            } else {
                //添加
                $user = User::where('id', $user->user_id)->first();
                $hasPermission = $user->hasPermissionTo('设置费率-' . $company);
                if (!$hasPermission) {
                    return json_encode(['status' => 2, 'message' => '没有权限设置费率']);
                }


                $status = 0;
                $status_desc = '未开通';
            }

            //查找下级门店共享通道
            if ($store->pid == 0) {
                $sub_store = Store::where('pid', $store->id)
                    ->where('pay_ways_type', 1)
                    ->select('store_id')
                    ->get();

                foreach ($sub_store as $k => $v) {
                    //暂时全部修改
                    $data['store_id'] = $v->store_id;
                    $data['rate'] = $rate;
                    $data['status'] = $status;
                    $data['status_desc'] = $status_desc;
                    $data['company'] = $company;
                    $this->send_ways_data($data);
                }
            }

            $company_config=DB::table('company_config')->where('store_id', $store_id)->where('company', $company)->first();
            if(empty($company_config)){
                return json_encode(['status' => 2, 'message' => '无通道配置,无法修改', 'data' => []]);
            }
            if($company_config->model=='4'){
                DB::table('company_config')->where('store_id', $store_id)->where('company', $company)->update(array('configure_channel_fee'=>$rate));
                DB::table('server_stores')->where('store_id', $store_id)->where('company', $company)->update(array('rata_store'=>$rate));
                DB::table('stores')->where('store_id', $store_id)->update(array('rata_store'=>$rate));
                //暂时全部修改
                $data['store_id'] = $store_id;
                $data['rate'] = '0.00';
                $data['status'] = $status;
                $data['status_desc'] = $status_desc;
                $data['company'] = $company;
                $return = $this->send_ways_data($data);
                return $return;
            }else if($company_config->model=='3'){
                DB::table('company_config')->where('store_id', $store_id)->where('company', $company)->update(array('configure_channel_fee'=>$rate));
                //暂时全部修改
                $data['store_id'] = $store_id;
                $data['rate'] = $rate;
                $data['status'] = $status;
                $data['status_desc'] = $status_desc;
                $data['company'] = $company;
                $return = $this->send_ways_data($data);
                return $return;
            }else if($company_config->model=='2'){
                DB::table('company_config')->where('store_id', $store_id)->where('company', $company)->update(array('configure_channel_fee'=>$rate));
                //暂时全部修改
                $data['store_id'] = $store_id;
                $data['rate'] = $rate;
                $data['status'] = $status;
                $data['status_desc'] = $status_desc;
                $data['company'] = $company;
                $return = $this->send_ways_data($data);
                return $return;
            }else if($company_config->model=='1'){
                DB::table('company_config')->where('store_id', $store_id)->where('company', $company)->update(array('configure_channel_fee'=>$rate));
                //暂时全部修改
                $data['store_id'] = $store_id;
                $data['rate'] = '0.00';
                $data['status'] = $status;
                $data['status_desc'] = $status_desc;
                $data['company'] = $company;
                $return = $this->send_ways_data($data);
                return $return;
            }
        } catch (\Exception $exception) {
            return json_encode(['status' => -1, 'message' => $exception->getMessage() . ' - ' . $exception->getLine()]);
        }

    }




//    public function edit_store_yinlian_rate_pc(Request $request)
//    {
//
//        try {
//            $user = $this->parseToken();
//            $store_id = $request->get('store_id');
//            $ways_type = $request->get('ways_type');
//            $rate_e = $request->get('rate_e');
//            $rate_f = $request->get('rate_f');
//            $rate_f_top = $request->get('rate_f_top', '20');
//
//            $rate_a=$request->get('rate_a');
//            $rate_c=$request->get('rate_c');
//
//            $store = Store::where('store_id', $store_id)
//                ->first();
//
//            if (!$store) {
//                return json_encode(['status' => 2, 'message' => '门店不存在', 'data' => []]);
//            }
//
//            //共享通道不支持修改费率
//            if ($store->pay_ways_type) {
//                return json_encode(['status' => 2, 'message' => '共享通道不支持修改费率']);
//            }
//
//
//            //代理商的费率
//            $user_rate = UserRate::where('user_id', $store->user_id)
//                ->where('ways_type', $ways_type)
//                ->select('rate_e', 'rate_f')
//                ->first();
//
//
//            if (!$user_rate) {
//                return json_encode(['status' => 2, 'message' => '代理商未设置费率', 'data' => []]);
//            }
//
//            //不能大于代理商的成本
//            if ($rate_e < $user_rate->rate_e) {
//                return json_encode(['status' => 2, 'message' => '费率不能低于代理商的费率', 'data' => []]);
//            }
//            //不能大于代理商的成本
//            if ($rate_f < $user_rate->rate_f) {
//                return json_encode(['status' => 2, 'message' => '费率不能低于代理商的费率', 'data' => []]);
//            }
//
//
//            //不能大于代理商的成本
//            if ($rate_a < $user_rate->rate_a) {
//                return json_encode(['status' => 2, 'message' => '费率不能低于代理商的费率', 'data' => []]);
//            }
//
//
//            //不能大于代理商的成本
//            if ($rate_c< $user_rate->rate_c) {
//                return json_encode(['status' => 2, 'message' => '费率不能低于代理商的费率', 'data' => []]);
//            }
//
//            $ways = StorePayWay::where('store_id', $store_id)->where('ways_type', $ways_type)
//                ->first();
//
//            if (!$ways) {
//                return json_encode(['status' => 2, 'message' => '请先设置扫码通道', 'data' => []]);
//            } else {
//                $status = $ways->status;
//                $status_desc = $ways->status_desc;
//
//                //如果通道审核中不允许修改费率
//                if ($status == 2) {
//                    return json_encode(['status' => 2, 'message' => '通道审核中不允许修改费率', 'data' => []]);
//                }
//
//                //新大陆同步平台费率
//                if ($user->level > 0 && $ways->status == 1 && 7999 < $ways_type && $ways_type < 8999) {
//                    $OBJ = new  UpdateController();
//                    //修改费率
//                    $up_data = [
//                        'store_id' => $store_id,
//                        'email' => $store->people_phone . '@139.com',
//                        'phone' => $store->people_phone,
//                        'rate' => $ways->rate,
//                        'rate_a' => $ways->rate_a,
//                        'rate_c' => $ways->rate_c,
//                        'rate_f' => $rate_f,
//                        'rate_f_top' => $rate_f_top,
//                        'rate_e' => $rate_e,
//                    ];
//
//                    //  $return = $OBJ->update_store_rate($up_data);
//                    // return $return;
//
//                    return json_encode(['status' => 2, 'message' => '暂不支持软件修改', 'data' => $request->except(['token'])]);
//
//                }
//
//
//                //京东收银通道修改费率
//                if ($user->level > 0 && $ways->status == 1 && 5999 < $ways_type && $ways_type < 6999) {
//                    return json_encode(['status' => 2, 'message' => '京东通道开通后不支持软件修改', 'data' => $request->except(['token'])]);
//                }
//            }
//
//
//            //全部修改
//            $company = $ways->company;
//            StorePayWay::where('store_id', $store_id)
//                ->where('company', $company)
//                ->update([
//                    'rate_a' => $rate_a,
//                    'rate_c' => $rate_c,
//                    'rate_e' => $rate_e,
//                    'rate_f' => $rate_f,
//                    'rate_f_top' => $rate_f_top,
//                ]);
//            $company_config=DB::table('company_config')->where('store_id', $store_id)->where('company', $company)->first();
//            if(empty($company_config)){
//                return json_encode(['status' => 2, 'message' => '无通道配置,无法修改', 'data' => []]);
//            }
//            if($company_config->model=='4'){
//                DB::table('company_config')->where('store_id', $store_id)->where('company', $company)->update(array('configure_channel_fee'=>$rate));
//                DB::table('server_stores')->where('store_id', $store_id)->where('company', $company)->update(array('rata_store'=>$rate));
//                DB::table('stores')->where('store_id', $store_id)->update(array('rata_store'=>$rate));
//                //暂时全部修改
//                $data['store_id'] = $store_id;
//                $data['rate'] = '0.00';
//                $data['status'] = $status;
//                $data['status_desc'] = $status_desc;
//                $data['company'] = $company;
//                $return = $this->send_ways_data($data);
//                return $return;
//            }else if($company_config->model=='3'){
//                DB::table('company_config')->where('store_id', $store_id)->where('company', $company)->update(array('configure_channel_fee'=>$rate));
//                //暂时全部修改
//                $data['store_id'] = $store_id;
//                $data['rate'] = $rate;
//                $data['status'] = $status;
//                $data['status_desc'] = $status_desc;
//                $data['company'] = $company;
//                $return = $this->send_ways_data($data);
//                return $return;
//            }else if($company_config->model=='2'){
//                DB::table('company_config')->where('store_id', $store_id)->where('company', $company)->update(array('configure_channel_fee'=>$rate));
//                //暂时全部修改
//                $data['store_id'] = $store_id;
//                $data['rate'] = $rate;
//                $data['status'] = $status;
//                $data['status_desc'] = $status_desc;
//                $data['company'] = $company;
//                $return = $this->send_ways_data($data);
//                return $return;
//            }else if($company_config->model=='1'){
//                DB::table('company_config')->where('store_id', $store_id)->where('company', $company)->update(array('configure_channel_fee'=>$rate));
//                //暂时全部修改
//                $data['store_id'] = $store_id;
//                $data['rate'] = '0.00';
//                $data['status'] = $status;
//                $data['status_desc'] = $status_desc;
//                $data['company'] = $company;
//                $return = $this->send_ways_data($data);
//                return $return;
//            }
//
//            return json_encode(['status' => 1, 'message' => '设置成功', 'data' => []]);
//
//
//        } catch (\Exception $exception) {
//            return json_encode(['status' => -1, 'message' => $exception->getMessage() . ' - ' . $exception->getLine()]);
//        }
//
//    }



    //门店设置费率-刷卡
    public function edit_store_un_rate(Request $request)
    {

        try {
            $user = $this->parseToken();
            $store_id = $request->get('store_id');
            $ways_type = $request->get('ways_type');
            $rate_e = $request->get('rate_e');
            $rate_f = $request->get('rate_f');
            $rate_f_top = $request->get('rate_f_top', '20');
            $store = Store::where('store_id', $store_id)
                ->first();

            if (!$store) {
                return json_encode(['status' => 2, 'message' => '门店不存在', 'data' => []]);
            }

            //共享通道不支持修改费率
            if ($store->pay_ways_type) {
                return json_encode(['status' => 2, 'message' => '共享通道不支持修改费率']);
            }


            //代理商的费率
            $user_rate = UserRate::where('user_id', $store->user_id)
                ->where('ways_type', $ways_type)
                ->select('rate_e', 'rate_f')
                ->first();


            if (!$user_rate) {
                return json_encode(['status' => 2, 'message' => '代理商未设置费率', 'data' => []]);
            }

            //不能大于代理商的成本
            if ($rate_e < $user_rate->rate_e) {
                return json_encode(['status' => 2, 'message' => '费率不能低于代理商的费率', 'data' => []]);
            }
            //不能大于代理商的成本
            if ($rate_f < $user_rate->rate_f) {
                return json_encode(['status' => 2, 'message' => '费率不能低于代理商的费率', 'data' => []]);
            }

            $ways = StorePayWay::where('store_id', $store_id)->where('ways_type', $ways_type)
                ->first();

            if (!$ways) {
                return json_encode(['status' => 2, 'message' => '请先设置扫码通道', 'data' => []]);
            } else {
                $status = $ways->status;
                $status_desc = $ways->status_desc;

                //如果通道审核中不允许修改费率
                if ($status == 2) {
                    return json_encode(['status' => 2, 'message' => '通道审核中不允许修改费率', 'data' => []]);
                }

                //新大陆同步平台费率
                if ($user->level > 0 && $ways->status == 1 && 7999 < $ways_type && $ways_type < 8999) {
                    $OBJ = new  UpdateController();
                    //修改费率
                    $up_data = [
                        'store_id' => $store_id,
                        'email' => $store->people_phone . '@139.com',
                        'phone' => $store->people_phone,
                        'rate' => $ways->rate,
                        'rate_a' => $ways->rate_a,
                        'rate_c' => $ways->rate_c,
                        'rate_f' => $rate_f,
                        'rate_f_top' => $rate_f_top,
                        'rate_e' => $rate_e,
                    ];

                    //  $return = $OBJ->update_store_rate($up_data);
                    // return $return;

                    return json_encode(['status' => 2, 'message' => '暂不支持软件修改', 'data' => $request->except(['token'])]);

                }


                //京东收银通道修改费率
                if ($user->level > 0 && $ways->status == 1 && 5999 < $ways_type && $ways_type < 6999) {
                    return json_encode(['status' => 2, 'message' => '京东通道开通后不支持软件修改', 'data' => $request->except(['token'])]);
                }
            }


            //全部修改
            $company = $ways->company;
            StorePayWay::where('store_id', $store_id)
                ->where('company', $company)
                ->update([
                    'rate_e' => $rate_e,
                    'rate_f' => $rate_f,
                    'rate_f_top' => $rate_f_top,
                ]);


            return json_encode(['status' => 1, 'message' => '设置成功', 'data' => []]);


        } catch (\Exception $exception) {
            return json_encode(['status' => -1, 'message' => $exception->getMessage() . ' - ' . $exception->getLine()]);
        }

    }


    //门店设置费率-银联扫码
    public function edit_store_unqr_rate(Request $request)
    {

        try {
            $user = $this->parseToken();
            $store_id = $request->get('store_id');
            $ways_type = $request->get('ways_type');
            $rate_a = $request->get('rate_a');
            $rate_b = $request->get('rate_b');
            $rate_b_top = $request->get('rate_b_top', '20');
            $rate_c = $request->get('rate_c');
            $rate_d = $request->get('rate_d');
            $rate_d_top = $request->get('rate_d_top', '20');
            $store = Store::where('store_id', $store_id)
                ->first();

            if (!$store) {
                return json_encode(['status' => 2, 'message' => '门店不存在', 'data' => []]);
            }

            //共享通道不支持修改费率
            if ($store->pay_ways_type) {
                return json_encode(['status' => 2, 'message' => '共享通道不支持修改费率']);
            }


            //代理商的费率
            $user_rate = UserRate::where('user_id', $store->user_id)
                ->where('ways_type', $ways_type)
                ->select('rate_a', 'rate_b')
                ->first();


            if (!$user_rate) {
                return json_encode(['status' => 2, 'message' => '代理商未设置费率', 'data' => []]);
            }

            //不能大于代理商的成本
            if ($rate_a < $user_rate->rate_a) {
                return json_encode(['status' => 2, 'message' => '小于1000贷记卡费率不能低于代理商的费率', 'data' => []]);
            }
            //不能大于代理商的成本
            if ($rate_b < $user_rate->rate_b) {
                return json_encode(['status' => 2, 'message' => '小于1000费率不能低于代理商的费率', 'data' => []]);
            }


            //不能大于代理商的成本
            if ($rate_c < $user_rate->rate_c) {
                return json_encode(['status' => 2, 'message' => '大于1000贷记卡费率不能低于代理商的费率', 'data' => []]);
            }
            //不能大于代理商的成本
            if ($rate_d < $user_rate->rate_d) {
                return json_encode(['status' => 2, 'message' => '大于1000借记卡费率不能低于代理商的费率', 'data' => []]);
            }

            $ways = StorePayWay::where('store_id', $store_id)->where('ways_type', $ways_type)
                ->first();

            if (!$ways) {
                return json_encode(['status' => 2, 'message' => '请先设置扫码通道', 'data' => []]);
            } else {
                $status = $ways->status;
                $status_desc = $ways->status_desc;

                //如果通道审核中不允许修改费率
                if ($status == 2) {
                    return json_encode(['status' => 2, 'message' => '通道审核中不允许修改费率', 'data' => []]);
                }

                //新大陆同步平台费率
                if ($user->level > 0 && $ways->status == 1 && 7999 < $ways_type && $ways_type < 8999) {
                    $OBJ = new  UpdateController();
                    //修改费率
                    $up_data = [
                        'store_id' => $store_id,
                        'email' => $store->people_phone . '@139.com',
                        'phone' => $store->people_phone,
                        'rate' => $ways->rate,
                        'rate_a' => $rate_a,
                        'rate_c' => $rate_c,
                        'rate_f' => $ways->rate_f,
                        'rate_f_top' => $ways->rate_f_top,
                        'rate_e' => $ways->rate_e,
                    ];

                    //$return = $OBJ->update_store_rate($up_data);
                    //  return $return;

                    return json_encode(['status' => 2, 'message' => '暂不支持软件修改', 'data' => $request->except(['token'])]);

                }


                //京东收银通道修改费率
                if ($user->level > 0 && $ways->status == 1 && 5999 < $ways_type && $ways_type < 6999) {
                    return json_encode(['status' => 2, 'message' => '京东通道开通后不支持软件修改', 'data' => $request->except(['token'])]);
                }
            }

            $company = $ways->company;

            //全部设置
            $update_data = [
                'rate_a' => $rate_a,
                'rate_b' => $rate_b,
                'rate_b_top' => $rate_b_top,
                'rate_c' => $rate_c,
                'rate_d' => $rate_d,
                'rate_d_top' => $rate_d_top,
            ];
            StorePayWay::where('store_id', $store_id)
                ->where('company', $company)
                ->update($update_data);


            return json_encode(['status' => 1, 'message' => '设置成功', 'data' => []]);


        } catch (\Exception $exception) {
            return json_encode(['status' => -1, 'message' => $exception->getMessage() . ' - ' . $exception->getLine()]);
        }

    }


    //pc端开通通道
    public function open_ways_type(Request $request)
    {
        try {
            $token = $this->parseToken();
            $pay_status = $request->get('pay_status', '1');
            $pay_status_desc = $request->get('pay_status_desc', '开通成功');
            $store_id = $request->get('store_id');
            $ways_type = $request->get('ways_type', '');

            //支付宝
            $alipay_store_id = $request->get('alipay_store_id', $store_id);
            $out_store_id = $request->get('out_store_id', $store_id);

            if ($alipay_store_id == "") {
                $alipay_store_id = "";
            }

            if ($out_store_id == "") {
                $out_store_id = "";
            }

            $user = User::where('id', $token->user_id)->first();
            $hasPermission = $user->hasPermissionTo('通道商户号');
            if (!$hasPermission) {
                return json_encode(['status' => 2, 'message' => '没有权限配置商户号']);
            }

            $weixin_path = $request->get('weixin_path', '');
            $weixin_path = isset($weixin_path) ? $weixin_path : "";

            //微信
            $wx_sub_merchant_id = $request->get('wx_sub_merchant_id', '');
            $wx_shop_id = $request->get('wx_shop_id', '');
            $wx_shop_id = isset($wx_shop_id) ? $wx_shop_id : "";
            $wxSharingOpen = $request->get('wxSharingOpen', ''); //是否开启分账
            $wxSharingType = $request->get('wxSharingType', ''); //分账接收方类型
            $wxSharingTccount = $request->get('wxSharingTccount', ''); //分账接收方帐号
            $wxSharingName = $request->get('wxSharingName', ''); //分账接收方全称
            $wxSharingRelationType = $request->get('wxSharingRelationType', ''); //与分账方的关系类型
            $wxSharingRate = $request->get('wxSharingRate', 0); //分账比例

            //京东
            $merchant_no = $request->get('merchant_no', '');
            $md_key = $request->get('md_key', '');
            $des_key = $request->get('des_key', '');

            //快钱
            $MerchantId = $request->get('MerchantId', '');

            //新大陆
            $nl_key = $request->get('nl_key', '');
            $nl_mercId = $request->get('nl_mercId', '');
            $trmNo = $request->get('trmNo', '');
            $wx_sub_appid = $request->post('wx_sub_appid', ''); //微信子公众号id

            //和融通
            $h_mid = $request->get('h_mid', '');

            //联拓富
            $appId = $request->get('appId', '');
            $ltf_md_key = $request->get('md_key', '');
            $merchantCode = $request->get('merchantCode', '');

            //哆啦宝
            $getdlbdata = $request->get('getdlbdata', '');
            $mch_num = $request->get('mch_num', '');
            $shop_num = $request->get('shop_num', '');
            $machine_num = $request->get('machine_num', '');

            //随行付
            $wx_channel_appid = $request->get('wx_channel_appid', ''); //微信渠道号
            $xcx_channel_appid = $request->get('xcx_channel_appid', ''); //小程序渠道号

            //汇付天下-商户号
            $user_cust_id= $request->get('mer_id', '');

            //农商行
            $nongjinkey=$request->get('nongjinkey', '');
            $zhongduanid=$request->get('zhongduanid', '');

            //平安银行
            $pingan_merchant_id=$request->get('pingan_merchant_id', '');
            $pingan_enterpriseReg=$request->get('pingan_enterpriseReg', '');
            $pingan_appid=$request->get('pingan_appid', '');
            $pingan_appsecret=$request->get('pingan_appsecret', '');

            //农行bmp
            $abckey=$request->get('abckey', '');
            $abczhongduanid=$request->get('abczhongduanid', '');
            $abcprivate_rsa_key=$request->get('abcprivate_rsa_key', '');
            $abcpublic_rsa_key=$request->get('abcpublic_rsa_key', '');

            //农行静态码
            $abc_quiet_merchant_id=$request->get('abc_quiet_merchant_id', '');
            $abc_quiet_cashier_id=$request->get('abc_quiet_cashier_id', '');
            $abc_quiet_private_key=$request->get('abc_quiet_private_key', '');
            $abc_quiet_public_key=$request->get('abc_quiet_public_key', '');

            //银联商务
            $merch_appid=$request->get('merch_appid', '');
            $merch_appkey=$request->get('merch_appkey', '');
            $merchzhongduanid=$request->get('merchzhongduanid', '');

            //通联支付
            $orgid=$request->get('orgid', '');
            $cusid=$request->get('cusid', '');
            $allinpay_appid=$request->get('allinpay_appid', '');
            $allinpay_pubkey=$request->get('allinpay_pubkey', '');
            $allinpay_prikey=$request->get('allinpay_prikey', '');

            //翼支付
            $wing_institutionCode=$request->get('wing_institutionCode', '');
            $wing_merchantNo=$request->get('wing_merchantNo', '');

            //扫呗
            $saobei_zhongduan=$request->get('saobei_zhongduan', '');
            $saobei_token=$request->get('saobei_token', '');
            $saobei_merchantNo=$request->get('saobei_merchantNo', '');

            //建行
            $ccb_merchantid=$request->get('ccb_merchantid', '');
            $ccb_posid=$request->get('ccb_posid', '');
            $ccb_bankchid=$request->get('ccb_bankchid', '');
            $ccb_pubkey=$request->get('ccb_pubkey', '');

            //甘肃农金
            $gsnongjin_merchant_id=$request->get('gsnongjin_merchant_id', '');

            //酷点
            $cool_merchantNo=$request->get('cool_merchantNo', '');
            $cool_secretkey=$request->get('cool_secretkey', '');

            //易生支付
            $easyPayOperaTrace = $request->get('easyPayOperaTrace', ''); //原操作流水号
            $easyPayMerCode = $request->get('easyPayMerCode', ''); //终端商户号
            $easyPayTermCode = $request->get('easyPayTermCode', ''); //终端终端号

            //葫芦
            $qd = $request->get('qd', '');

            //长沙银行
            $DeptId = $request->get('DeptId', '');
            $StaffId = $request->get('StaffId', '');

            //邮政
            $pos_sn = $request->get('pos_sn', '');
            $signkey = $request->get('signkey', '');

            //联动优势
            $linkageAcqMerId = $request->get('linkageAcqMerId', ''); //商户号
            $linkageSubAppid = $request->get('linkageSubAppid', ''); //子商户号
            $linkageSubMchId = $request->get('linkageSubMchId', ''); //子商户号
            $linkageMerId = $request->get('linkageMerId', ''); //报备编号

            //威富通
            $wftpayMchId = $request->get('wftpayMchId', ''); //商户号
            $hwcpayMchId = $request->get('hwcpayMchId', ''); //汇旺财门店号
            $hwcpayMerchantNum = $request->get('hwcpayMerchantNum', ''); //汇旺财商户号

            //海科融通
            $hkrtMerchNo = $request->get('hkrtMerchNo', ''); //商户号
            $hkrtAgentApplyNo = $request->get('hkrtAgentApplyNo', ''); //服务商申请单号

            // 九江银行
            $jiujiang_mchtNo = $request->get('jiujiang_mchtNo', ''); // 商户号
            $jiujiang_termNo = $request->get('jiujiang_termNo', ''); // 终端号
            $jiujiang_private_key = $request->get('jiujiang_private_key', ''); // 商户私钥

            // 东莞银行
            $dongguan_mchtNo = $request->get('dongguan_mchtNo', ''); // 商户号
            $dongguan_termNo = $request->get('dongguan_termNo', ''); // 终端号
            $dongguan_private_key = $request->get('dongguan_private_key', ''); // 商户私钥

            $Store = Store::where('store_id', $store_id)
                ->first();
            if (!$Store) {
                return json_encode([
                    'status' => 2,
                    'message' => '门店不存在',
                ]);
            }

            $config_id = $Store->config_id;
            $store_name = $Store->store_name;
            $store_pid = $Store->pid;

            //费率 默认商户的费率为代理商的费率
            $company = $request->get('company', '');
            $UserRate = UserRate::query()->where('user_id', $Store->user_id)
                ->where('ways_type', $ways_type) //目前是一样的直接读取支付宝就行
//                ->where("company", $company)
                ->first();
            if (!$UserRate) {
                return json_encode([
                    'status' => 2,
                    'message' => '请联系代理商开启此通道',
                ]);
            }

            $rate = $UserRate->store_all_rate;




            // $this->operation_log( $token->user_id, 4, '门店商户号: 门店id: ' . $store_id . '修改商户号, 通道type为: ' . $ways_type ); // 操作日志
            $pash = $request->path();
            $pass = $request->get('pass', '');
            // print_r($pass);die;
            $pass = substr(base64_decode($pass), 0, -5);
            $pass_verify = $this->pass_verify($pass, $pash); // 密钥验证

            $pass_verify = json_decode($pass_verify, true);
            // print_r( $this->parseToken() );die;
            if( $pass_verify['status'] == '1'){
                $pass_id = DB::table('pass')->where(['pass' => $pass])->value('id'); // 密钥id
                $remark = '门店商户号: 门店id: ' . $store_id . '修改商户号, 通道type为: ' . $ways_type."参数:".json_encode($request->all());
                $this->operation_log($this->parseToken()->user_id, 4, $remark , $pass_id);
            }else{
                return json_encode(['status' => 2, 'message' => '验证不通过', 'data' => []]);
            }


            // 九江银行
            if ('37001' == $ways_type || '37002' == $ways_type || '37003' == $ways_type) {
                $data = DB::table('jiujiang_stores')->where('store_id', $store_id)->select('*')->first();

                if ($MerchantId == '') {
                    if ($data) {
                        return json_encode([
                            'status' => 1,
                            'data' => $data
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    $in = [
                        'config_id' => '1234',
                        'store_id' => $store_id,
                        'mchtNo' => $jiujiang_mchtNo,
                        'termNo' => $jiujiang_termNo,
                        'private_key' => $jiujiang_private_key,
                    ];
                    if ($data) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type
                            , [
                                "user_id"=>$token->user_id
                            ]
                        );
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        DB::table('jiujiang_stores')->where('store_id', $store_id)->update($in);
                    } else {
                        DB::table('jiujiang_stores')->insert($in);
                    }

                    DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'jiujiang')->update(array('status'=>1,'status_desc'=>'开通成功'));
                    $return = array('status'=>1,'message'=>'修改成功');

                    return $return;
                }


            }

            //农商行
            if ('20801' == $ways_type || '20802' == $ways_type ||'20803' == $ways_type) {
                $data = NongjinStores::where('store_id', $store_id)
                    ->select('*')
                    ->first();

                if ($MerchantId == '') {
                    if ($data) {
                        return json_encode([
                            'status' => 1,
                            'data' => $data
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    $in = [
                        'store_id' => $store_id,
                        'config_id' => $config_id,
                        'merchantNo' => $MerchantId,
                        'signkey' => $nongjinkey,
                        'terminalNo' => $zhongduanid,
                    ];
                    if ($data) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type
                            , [
                                "user_id"=>$token->user_id
                            ]
                        );
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        NongjinStores::where('store_id', $store_id)->update($in);
                    } else {
                        NongjinStores::create($in);
                    }

                    //默认支付通道未注册成功
                    // $data['store_id'] = $store_id;
                    // $data['rate'] = $rate;
                    // $data['status'] = $pay_status;
                    // $data['status_desc'] = $pay_status_desc;
                    // $data['company'] = 'nongjin';
                    // $return = $this->send_ways_data($data);
                    DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'nongjin')->update(array('status'=>1,'status_desc'=>'开通成功'));
                    $return = array('status'=>1,'message'=>'修改成功');

                    $post_data = [
                        'merchantNo' => $MerchantId,
                        'terminalNo' => $zhongduanid
                    ];
                    $url = 'https://epos.ahrcu.com:3443/cposp/pay/signIn';
                    Log::channel("pay")->info("农商行请求参数",[$post_data]);
                    $re = $this->http_curl($url, json_encode($post_data));
                    Log::channel("pay")->info("农商行请求结果",[$re]);
                    $re = json_decode($re, true);
                    if($re['resultCode']=='00'){
                        DB::table('nongjin_stores')->where('store_id', $store_id)->update(array('batchNo'=>$re['batchNo']));
                    }

                    return $return;
                }
            }

            //平安银行
            if ('30011' == $ways_type || '30012' == $ways_type ||'30013' == $ways_type) {
                $data = PinganStores::where('store_id', $store_id)
                    ->select('*')
                    ->first();

                if ($MerchantId == '') {
                    if ($data) {
                        return json_encode([
                            'status' => 1,
                            'data' => $data
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    $in = [
                        'store_id' => $store_id,
                        'config_id' => $config_id,
                        'merchant_id' => $pingan_merchant_id,
                        'enterpriseReg' => $pingan_enterpriseReg,
                        'appid' => $pingan_appid,
                        'appsecret'=>$pingan_appsecret
                    ];

                    if ($data) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type
                            , [
                                "user_id"=>$token->user_id
                            ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        PinganStores::where('store_id', $store_id)->update($in);
                    } else {
                        PinganStores::create($in);
                    }

                    // $obj = new \App\Api\Controllers\lianfu\PayController();
                    // $pingan_data['merchant_id']=$pingan_merchant_id;

                    // foreach (array_filter($pingan_data) as $k => $v){
                    //     $Parameters[$k] = $v;
                    // }
                    // $buff = "";
                    // ksort($Parameters);
                    // foreach ($Parameters as $k => $v) {
                    //     $buff .= $k . "=" . $v . "&";
                    // }
                    // $String = '';
                    // if (strlen($buff) > 0) {
                    //     $String = substr($buff, 0, strlen($buff) - 1);
                    // }
                    // $String = $String . $pingan_appsecret;
                    // $re=hash('sha256', $String, true);
                    // $String = strtoupper(bin2hex($re));
                    // $json='{"appid":"'."$pingan_appid".'"'.',"timeStamp":"'.time().'000'.'"'.',"data":'.stripslashes(json_encode($pingan_data,JSON_UNESCAPED_SLASHES)).',"sign":"'.$String.'"'.'}';
                    // $return = $obj->postpingan($json);
//                     file_put_contents('pingantuisong.log',var_export($store_id.'-'.$return,true)."\r\n",FILE_APPEND);
// //                    echo '<pre>';
// //                    print_r($return);
// //                    die;
                    //默认支付通道未注册成功
                    // $data['store_id'] = $store_id;
                    // $data['rate'] = $rate;
                    // $data['status'] = $pay_status;
                    // $data['status_desc'] = $pay_status_desc;
                    // $data['company'] = 'pingan';
                    // $return = $this->send_ways_data($data);
                    DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'pingan')->update(array('status'=>1,'status_desc'=>'开通成功'));
                    $return = array('status'=>1,'message'=>'修改成功');

                    return $return;
                }
            }

            //甘肃农金
            if ('20851' == $ways_type || '20852' == $ways_type ||'20853' == $ways_type) {
                $data = GsNongjinStores::where('store_id', $store_id)
                    ->select('*')
                    ->first();

                if ($MerchantId == '') {
                    if ($data) {
                        return json_encode([
                            'status' => 1,
                            'data' => $data
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    $in = [
                        'store_id' => $store_id,
                        'config_id' => $config_id,
                        'MerchantId' => $gsnongjin_merchant_id,
                    ];
                    if ($data) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        GsNongjinStores::where('store_id', $store_id)->update($in);
                    } else {
                        GsNongjinStores::create($in);
                    }

                    //默认支付通道未注册成功
                    // $data['store_id'] = $store_id;
                    // $data['rate'] = $rate;
                    // $data['status'] = $pay_status;
                    // $data['status_desc'] = $pay_status_desc;
                    // $data['company'] = 'gs_nongjin';
                    // $return = $this->send_ways_data($data);
                    DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'gs_nongjin')->update(array('status'=>1,'status_desc'=>'开通成功'));
                    $return = array('status'=>1,'message'=>'修改成功');

                    return $return;
                }
            }

            //农行bmp
            if ('30001' == $ways_type || '30002' == $ways_type ||'30003' == $ways_type) {
                $data = AbcStores::where('store_id', $store_id)
                    ->select('*')
                    ->first();

                if ($MerchantId == '') {
                    if ($data) {
                        return json_encode([
                            'status' => 1,
                            'data' => $data
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    $in = [
                        'store_id' => $store_id,
                        'config_id' => $config_id,
                        'MerchantId' => $MerchantId,
                        'abckey' => $abckey,
                        'abczhongduanid' => $abczhongduanid,
                        'private_rsa_key' => $abcprivate_rsa_key,
                        'public_rsa_key' => $abcpublic_rsa_key,
                    ];
                    if ($data) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        AbcStores::where('store_id', $store_id)->update($in);
                    } else {
                        AbcStores::create($in);
                    }

                    //默认支付通道未注册成功
                    // $data['store_id'] = $store_id;
                    // $data['rate'] = $rate;
                    // $data['status'] = $pay_status;
                    // $data['status_desc'] = $pay_status_desc;
                    // $data['company'] = 'abc';
                    // $return = $this->send_ways_data($data);
                    DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'abc')->update(array('status'=>1,'status_desc'=>'开通成功'));
                    $return = array('status'=>1,'message'=>'修改成功');

                    return $return;
                }
            }

            //农行静态码
            if ('30004' == $ways_type || '30005' == $ways_type ||'30006' == $ways_type) {
                $data = AbcquietStores::where('store_id', $store_id)
                    ->select('*')
                    ->first();

                if ($MerchantId == '') {
                    if ($data) {
                        return json_encode([
                            'status' => 1,
                            'data' => $data
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    $in = [
                        'store_id' => $store_id,
                        'config_id' => $config_id,
                        'merchant_id' => $abc_quiet_merchant_id,
                        'cashier_id' => $abc_quiet_cashier_id,
                        'private_key' => $abc_quiet_private_key,
                        'public_key' => $abc_quiet_public_key,
                    ];
                    if ($data) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        AbcquietStores::where('store_id', $store_id)->update($in);
                    } else {
                        AbcquietStores::create($in);
                    }

                    //默认支付通道未注册成功
                    // $data['store_id'] = $store_id;
                    // $data['rate'] = $rate;
                    // $data['status'] = $pay_status;
                    // $data['status_desc'] = $pay_status_desc;
                    // $data['company'] = 'abc_quiet';
                    // $return = $this->send_ways_data($data);
                    DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'abc_quiet')->update(array('status'=>1,'status_desc'=>'开通成功'));
                    $return = array('status'=>1,'message'=>'修改成功');

                    return $return;
                }
            }

            //通联支付
            if ('32000' == $ways_type || '32001' == $ways_type ||'32002' == $ways_type) {
                $data = AllinpayStores::where('store_id', $store_id)
                    ->select('*')
                    ->first();
                if ($MerchantId == '') {
                    if ($data) {
                        return json_encode([
                            'status' => 1,
                            'data' => $data
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    $in = [
                        'store_id' => $store_id,
                        'config_id' => $config_id,
                        'orgid' => empty($orgid)?0:$orgid,
                        'cusid' => $cusid,
                        'appid' => $allinpay_appid,
                        'pubkey' => $allinpay_pubkey,
                        'prikey' => $allinpay_prikey,
                    ];
                    if ($data) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        AllinpayStores::where('store_id', $store_id)->update($in);
                    } else {
                        AllinpayStores::create($in);
                    }

                    //默认支付通道未注册成功
                    // $data['store_id'] = $store_id;
                    // $data['rate'] = $rate;
                    // $data['status'] = $pay_status;
                    // $data['status_desc'] = $pay_status_desc;
                    // $data['company'] = 'allinpay';
                    // $return = $this->send_ways_data($data);
                    DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'allinpay')->update(array('status'=>1,'status_desc'=>'开通成功'));
                    $return = array('status'=>1,'message'=>'修改成功');

                    if(!empty($orgid)){
                        $post_data['orgid']=$orgid;
                    }
                    $termno=rand(10000000,99999999);

                    $post_data['cusid']=$cusid;
                    $post_data['appid']=$allinpay_appid;
                    $post_data['version']='12';
                    $post_data['termno']=$termno;
                    $post_data['devicetype']='13';
                    $post_data['operation']='00';
                    $post_data['termstate']='00';
                    $post_data['termaddress']=$Store->province_name.'-'.$Store->city_name.'-'.$Store->area_name.'-'.$Store->store_address;
                    $post_data['signtype']='RSA';
                    $post_data['sign']=urlencode($this->sign($post_data,$allinpay_prikey));
                    $paramsStr=$this->ToUrlParams($post_data);
//                    file_put_contents('b77.log',var_export($paramsStr,true)."\r\n",FILE_APPEND);
                    $url = 'https://vsp.allinpay.com/cusapi/merchantapi/addterm';
                    $re = $this->request($url, $paramsStr);
//                    $re="{\"appid\":\"00012395\",\"cusid\":\"990361082416001\",\"randomstr\":\"472511595287\",\"retcode\":\"SUCCESS\",\"retmsg\":\"受理成功\",\"sign\":\"gQZllfO+QowPWNCfQRaaUM4VRffUqYaz31CE+kZQbOgK+JpQOw3uh78SRYatz9qVOvDvIaNEQKb8QMRzXOjX0T7YUINTGmDLm8b4sRLkR9mF9NQmKba9zumb6DauUdZyX8NT48s56+sEO8pfJbIC37TIZrUDXqKsIR8d+ePTUqc=\"}";
                    error_log(date('Y-m-d H:i:s') . "\t----xxx----:". json_encode($post_data) . "\n", 3, "/tmp/test".date('Ymd').".log");

                    $re = json_decode($re, true);
                    error_log(date('Y-m-d H:i:s') . "\t----xxx----:". json_encode($re) . "\n", 3, "/tmp/test".date('Ymd').".log");

                    if($re['retcode']=='SUCCESS'){
                        DB::table('allinpay_stores')->where('store_id', $store_id)->update(array('termno'=>$termno));
                    }
                    return $return;
                }
            }

            //银联商务
            if ('30020' == $ways_type || '30021' == $ways_type ||'30022' == $ways_type) {
                $data = MerchStores::where('store_id', $store_id)
                    ->where("version","v1")
                    ->select('*')
                    ->first();

                if ($MerchantId == '') {
                    if ($data) {
                        return json_encode([
                            'status' => 1,
                            'data' => $data
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    $in = [
                        'store_id' => $store_id,
                        'config_id' => $config_id,
                        'MerchantId' => $MerchantId,
                        'merch_appid' => $merch_appid,
                        'merch_appkey' => $merch_appkey,
                        'merchzhongduanid' => $merchzhongduanid,
                        "version"=>"v1"
                    ];
                    if ($data) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        MerchStores::where('store_id', $store_id)->where("version","v1")->update($in);
                    } else {
                        MerchStores::create($in);
                    }

                    $nonce=$this->nonceStr();
                    $appId=$merch_appid;
                    $timestamp=date('Ymdhis', time());
                    $signMethod='SHA256';
                    $appKey=$merch_appkey;
                    $data_info=array(
                        'appId'=>$appId,
                        'timestamp'=>$timestamp,
                        'nonce'=>$nonce,
                        'signMethod'=>$signMethod,
                    );
                    $data_info['signature']=$this->signature($data_info);
                    $result = json_decode($this->curl_merch('https://api-mop.chinaums.com/v1/token/access' ,'POST',json_encode($data_info),['Content-type: application/json']),true);
                    if(isset($result['accessToken'])){
                        $res=DB::table('merch_stores')->where('store_id',$store_id)->update(array('accessToken'=>$result['accessToken']));
                        $data_in=array(
                            'merchantCode'=>$MerchantId,
                            'terminalCode'=>$merchzhongduanid
                        );
                        $data_res = $this->curl_merch('https://api-mop.chinaums.com/v2/poslink/transaction/activeterminal','POST',json_encode($data),['Content-type: application/json','Authorization:OPEN-ACCESS-TOKEN AccessToken='.$result['accessToken']]);
                    }

                    //默认支付通道未注册成功
                    // $data['store_id'] = $store_id;
                    // $data['rate'] = $rate;
                    // $data['status'] = $pay_status;
                    // $data['status_desc'] = $pay_status_desc;
                    // $data['company'] = 'unionpay_merch';
                    // $return = $this->send_ways_data($data);
                    DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'unionpay_merch')->update(array('status'=>1,'status_desc'=>'开通成功'));
                    $return = array('status'=>1,'message'=>'修改成功');

                    return $return;
                }
            }


            //银联商务V2
            if ('30023' == $ways_type || '30024' == $ways_type ||'30025' == $ways_type) {
                $data = MerchStores::where('store_id', $store_id)
                    ->where("version","v2")
                    ->select('*')
                    ->first();

                if ($MerchantId == '') {
                    if ($data) {
                        return json_encode([
                            'status' => 1,
                            'data' => $data
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    $in = [
                        "version" => "v2",
                        'store_id' => $store_id,
                        'config_id' => $config_id,
                        'MerchantId' => $MerchantId,
                        'merch_appid' => $merch_appid,
                        'merch_appkey' => $merch_appkey,
                        'merchzhongduanid' => $merchzhongduanid,
                    ];
                    if ($data) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        MerchStores::where('store_id', $store_id)->where("version","v2")->update($in);
                    } else {
                        MerchStores::create($in);
                    }

                    //默认支付通道未注册成功
                    // $data['store_id'] = $store_id;
                    // $data['rate'] = $rate;
                    // $data['status'] = $pay_status;
                    // $data['status_desc'] = $pay_status_desc;
                    // $data['company'] = 'unionpay_merch';
                    // $return = $this->send_ways_data($data);
                    DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'unionpay_merch_v2')->update(array('status' => 1, 'status_desc' => '开通成功'));
                    $return = array('status' => 1, 'message' => '修改成功');

                    return $return;
                }
            }

            //翼支付
            if ('30050' == $ways_type || '30051' == $ways_type || '30052' == $ways_type || '30053' == $ways_type) {
                $data = WingStores::where('store_id', $store_id)
                    ->select('*')
                    ->first();
                if ($MerchantId == '') {
                    if ($data) {
                        return json_encode([
                            'status' => 1,
                            'data' => $data
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    $in = [
                        'store_id' => $store_id,
                        'config_id' => $config_id,
                        'institutionCode' => $wing_institutionCode,
                        'merchantNo' => $wing_merchantNo,
                    ];
                    if ($data) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        WingStores::where('store_id', $store_id)->update($in);
                    } else {
                        WingStores::create($in);
                    }

                    //默认支付通道未注册成功
                    // $data['store_id'] = $store_id;
                    // $data['rate'] = $rate;
                    // $data['status'] = $pay_status;
                    // $data['status_desc'] = $pay_status_desc;
                    // $data['company'] = 'wingpay';
                    // $return = $this->send_ways_data($data);
                    DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'wingpay')->update(array('status'=>1,'status_desc'=>'开通成功'));
                    $return = array('status'=>1,'message'=>'修改成功');

                    return $return;
                }
            }

            //扫呗
            if ('30080' == $ways_type || '30081' == $ways_type || '30082' == $ways_type) {
                $data = SaobeiStores::where('store_id', $store_id)
                    ->select('*')
                    ->first();
                if ($MerchantId == '') {
                    if ($data) {
                        return json_encode([
                            'status' => 1,
                            'data' => $data
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    $in = [
                        'store_id' => $store_id,
                        'config_id' => $config_id,
                        'zhongduan' => $saobei_zhongduan,
                        'merchantNo' => $saobei_merchantNo,
                        'access_token' => $saobei_token,
                    ];
                    if ($data) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        SaobeiStores::where('store_id', $store_id)->update($in);
                    } else {
                        SaobeiStores::create($in);
                    }

                    //默认支付通道未注册成功
                    // $data['store_id'] = $store_id;
                    // $data['rate'] = $rate;
                    // $data['status'] = $pay_status;
                    // $data['status_desc'] = $pay_status_desc;
                    // $data['company'] = 'saobei';
                    // $return = $this->send_ways_data($data);
                    DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'saobei')->update(array('status'=>1,'status_desc'=>'开通成功'));
                    $return = array('status'=>1,'message'=>'修改成功');

                    return $return;
                }
            }

            //建行
            if ('36001' == $ways_type || '36002' == $ways_type || '36003' == $ways_type) {
                $data = CcbStores::where('store_id', $store_id)
                    ->select('*')
                    ->first();
                if ($MerchantId == '') {
                    if ($data) {
                        return json_encode([
                            'status' => 1,
                            'data' => $data
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    $in = [
                        'store_id' => $store_id,
                        'config_id' => $config_id,
                        'merchantid' => $ccb_merchantid,
                        'posid' => $ccb_posid,
                        'bankchid' => $ccb_bankchid,
                        'pubkey' => $ccb_pubkey,
                    ];
                    if ($data) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        CcbStores::where('store_id', $store_id)->update($in);
                    } else {
                        CcbStores::create($in);
                    }

                    //默认支付通道未注册成功
                    // $data['store_id'] = $store_id;
                    // $data['rate'] = $rate;
                    // $data['status'] = $pay_status;
                    // $data['status_desc'] = $pay_status_desc;
                    // $data['company'] = 'ccb';
                    // $return = $this->send_ways_data($data);
                    DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'ccb')->update(array('status'=>1,'status_desc'=>'开通成功'));
                    $return = array('status'=>1,'message'=>'修改成功');

                    return $return;
                }
            }

            //酷点
            if ('30090' == $ways_type || '30091' == $ways_type || '30092' == $ways_type) {
                $data = CoolStores::where('store_id', $store_id)
                    ->select('*')
                    ->first();
                if ($MerchantId == '') {
                    if ($data) {
                        return json_encode([
                            'status' => 1,
                            'data' => $data
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    $in = [
                        'store_id' => $store_id,
                        'config_id' => $config_id,
                        'merchantNo' => $cool_merchantNo,
                        'secret_key' => $cool_secretkey
                    ];
                    if ($data) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        CoolStores::where('store_id', $store_id)->update($in);
                    } else {
                        CoolStores::create($in);
                    }

                    //默认支付通道未注册成功
                    // $data['store_id'] = $store_id;
                    // $data['rate'] = $rate;
                    // $data['status'] = $pay_status;
                    // $data['status_desc'] = $pay_status_desc;
                    // $data['company'] = 'cool';
                    // $return = $this->send_ways_data($data);
                    DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'cool')->update(array('status'=>1,'status_desc'=>'开通成功'));
                    $return = array('status'=>1,'message'=>'修改成功');

                    return $return;
                }
            }


            //支付宝
            if ($ways_type == '1000') {
                $storeInfo = AlipayAppOauthUsers::where('store_id', $store_id)->first();
                if ($alipay_store_id == "" && $out_store_id) {
                    if ($storeInfo) {
                        return json_encode([
                            'status' => 1,
                            'data' => [
                                'alipay_store_id' => $storeInfo->alipay_store_id,
                                'out_store_id' => $storeInfo->out_store_id,
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    if ($storeInfo) {
                        $storeInfo->alipay_store_id = $alipay_store_id;
                        $storeInfo->out_store_id = $out_store_id;
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        $storeInfo->save();
                    } else {
                        //添加
                        $insert_data = [
                            'alipay_user_id' => '',
                            'alipay_store_id' => $alipay_store_id,
                            'out_store_id' => $out_store_id,
                            'merchant_id' => $Store->merchant_id,
                            'alipay_user_account' => '',
                            'alipay_user_account_name' => '',
                            'config_type' => '01',
                            'pid' => $Store->pid,
                            'store_id' => $store_id,
                            'is_delete' => 0,
                            'auth_app_id' => '',
                            'app_auth_token' => '',
                            'app_refresh_token' => '',
                            'expires_in' => '',
                            're_expires_in' => '',
                            'trade_pay_rate' => 0.6,
                        ];
                        AlipayAppOauthUsers::create($insert_data);
                    }
                }
            }

            //微信官方
            if ($ways_type == '2000') {
                $WeixinStoreObj = WeixinStore::where('store_id', $store_id)
//                    ->select('wx_sub_merchant_id', 'wx_shop_id')
                    ->first();
                if (!$WeixinStoreObj) {
                    //看下是否有修改权限
                    $hasPermission = $user->hasPermissionTo('微信商户号添加');
                    if (!$hasPermission) {
                        return json_encode(['status' => 2, 'message' => '没有权限']);
                    }
                } else {
                    //看下是否有添加权限
                    $hasPermission = $user->hasPermissionTo('微信商户号修改');
                    if (!$hasPermission) {
                        return json_encode(['status' => 2, 'message' => '没有权限修改']);
                    }
                }

                if ($wx_sub_merchant_id == '') {
                    if ($WeixinStoreObj) {
                        return json_encode([
                            'status' => 1,
                            'data' => $WeixinStoreObj
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    if ($WeixinStoreObj) {
                        $wx_store_update = [
                            'config_id' => $config_id,
                            'store_id' => $store_id,
                            'status' => 1,
                            'status_desc' => '成功',
                            'wx_shop_id' => $wx_shop_id,
                            'wx_sub_merchant_id' => $wx_sub_merchant_id,
                        ];
                        if ($wxSharingOpen) {
                            $wx_store_update['is_profit_sharing'] = $wxSharingOpen;
                            if ($wxSharingType) $wx_store_update['wx_sharing_type'] = $wxSharingType;
                            if ($wxSharingTccount) $wx_store_update['wx_sharing_account'] = $wxSharingTccount;
                            if ($wxSharingName) $wx_store_update['wx_sharing_name'] = $wxSharingName;
                            if ($wxSharingRelationType) $wx_store_update['wx_sharing_relation_type'] = $wxSharingRelationType;
                            if ($wxSharingRate) $wx_store_update['wx_sharing_rate'] = $wxSharingRate;

                            //添加分账接收方失败，再添加一次
                            if ($WeixinStoreObj->wx_sharing_status == 0 && $wxSharingType) {
                                $config = new WeixinConfigController();
                                $options = $config->weixin_config($config_id);

                                $profit_sharing_add_receiver_data = [
                                    'store_id' => $store_id,
                                    'options' => $options,
                                    'wx_sub_merchant_id' => $wx_sub_merchant_id,
                                    'ways_type' => $ways_type
                                ];
                                $wx_pay_obj = new WeixinPayController();
                                $res = $wx_pay_obj->profit_sharing_add_receiver($profit_sharing_add_receiver_data);
                            }
                        }
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        $WeixinStoreObj->update($wx_store_update);
                        DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'weixin')->update(array('status'=>1,'status_desc'=>'开通成功'));
                    } else {
                        $gets2 = StorePayWay::where('store_id', $store_id)->where('ways_source', 'weixin')->get();
                        $ways = StorePayWay::where('store_id', $store_id)->where('ways_type', 2000)->first();
                        $count = count($gets2);

                        $rate = 0.6;
                        $store = Store::where('store_id', $store_id)
                            ->select('user_id')
                            ->first();
                        if (!$store) {
                            return json_encode([
                                'status' => 2,
                                'msg' => '门店不存在'
                            ]);
                        }

                        // $UserRate = UserRate::where('user_id', $store->user_id)
                        //     ->where('ways_type', $ways_type)
                        //     ->first();
                        // if ($UserRate) {
                        //     $rate = $UserRate->rate;
                        // }
                        // $data1 = [
                        //     'store_id' => $store_id,
                        //     'ways_type' => 2000,
                        //     'ways_source' => 'weixin',
                        //     'ways_desc' => '微信支付',
                        //     'sort' => ($count + 1),
                        //     'status' => 1,
                        //     'status_desc' => '成功',
                        //     'rate' => $rate,
                        //     'settlement_type' => 'T1',
                        //     'company' => 'weixin',
                        // ];
                        // if ($ways) {
                        //     $ways->update($data1);
                        //     $ways->save();
                        // } else {
                        //     StorePayWay::create($data1);
                        // }
                        DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'weixin')->update(array('status'=>1,'status_desc'=>'开通成功'));

                        $wx_store_create = [
                            'config_id' => $config_id,
                            'store_id' => $store_id,
                            'status' => 1,
                            'status_desc' => '成功',
                            'wx_sub_merchant_id' => $wx_sub_merchant_id,
                            'wx_shop_id' => $wx_shop_id,
                        ];
                        if ($wxSharingOpen) {
                            $wx_store_create['is_profit_sharing'] = $wxSharingOpen;
                            if ($wxSharingType) $wx_store_create['wx_sharing_type'] = $wxSharingType;
                            if ($wxSharingTccount) $wx_store_create['wx_sharing_account'] = $wxSharingTccount;
                            if ($wxSharingName) $wx_store_create['wx_sharing_name'] = $wxSharingName;
                            if ($wxSharingRelationType) $wx_store_create['wx_sharing_relation_type'] = $wxSharingRelationType;
                            if ($wxSharingRate) $wx_store_create['wx_sharing_rate'] = $wxSharingRate;
                        }
                        $result = WeiXinStore::create($wx_store_create);
                        if ($result) {
                            //添加分账接收方
                            $config = new WeixinConfigController();
                            $options = $config->weixin_config($config_id);

                            $profit_sharing_add_receiver_data = [
                                'store_id' => $store_id,
                                'options' => $options,
                                'wx_sub_merchant_id' => $wx_sub_merchant_id,
                                'ways_type' => $ways_type
                            ];
                            $wx_pay_obj = new WeixinPayController();
                            $res = $wx_pay_obj->profit_sharing_add_receiver($profit_sharing_add_receiver_data);
                        }
                    }
                }
            }

            //微信官方a
            if ($ways_type == '4000') {
                $WeixinStoreObj = WeixinaStore::where('store_id', $store_id)
//                    ->select('wx_sub_merchant_id', 'wx_shop_id')
                    ->first();
                if (!$WeixinStoreObj) {
                    //看下是否有修改权限
                    $hasPermission = $user->hasPermissionTo('微信商户号添加');
                    if (!$hasPermission) {
                        return json_encode(['status' => 2, 'message' => '没有权限']);
                    }
                } else {
                    //看下是否有添加权限
                    $hasPermission = $user->hasPermissionTo('微信商户号修改');
                    if (!$hasPermission) {
                        return json_encode(['status' => 2, 'message' => '没有权限修改']);
                    }
                }

                if ($wx_sub_merchant_id == '') {
                    if ($WeixinStoreObj) {
                        return json_encode([
                            'status' => 1,
                            'data' => $WeixinStoreObj
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    if ($WeixinStoreObj) {
                        $wx_store_update = [
                            'config_id' => $config_id,
                            'store_id' => $store_id,
                            'status' => 1,
                            'status_desc' => '成功',
                            'wx_shop_id' => $wx_shop_id,
                            'wx_sub_merchant_id' => $wx_sub_merchant_id,
                        ];
                        if ($wxSharingOpen) {
                            $wx_store_update['is_profit_sharing'] = $wxSharingOpen;
                            if ($wxSharingType) $wx_store_update['wx_sharing_type'] = $wxSharingType;
                            if ($wxSharingTccount) $wx_store_update['wx_sharing_account'] = $wxSharingTccount;
                            if ($wxSharingName) $wx_store_update['wx_sharing_name'] = $wxSharingName;
                            if ($wxSharingRelationType) $wx_store_update['wx_sharing_relation_type'] = $wxSharingRelationType;
                            if ($wxSharingRate) $wx_store_update['wx_sharing_rate'] = $wxSharingRate;

                            //添加分账接收方失败，再添加一次
                            if ($WeixinStoreObj->wx_sharing_status == 0 && $wxSharingType) {
                                $config = new WeixinConfigController();
                                $options = $config->weixina_config($config_id);

                                $profit_sharing_add_receiver_data = [
                                    'store_id' => $store_id,
                                    'options' => $options,
                                    'wx_sub_merchant_id' => $wx_sub_merchant_id,
                                    'ways_type' => $ways_type
                                ];
                                $wx_pay_obj = new WeixinPayController();
                                $res = $wx_pay_obj->profit_sharing_add_receiver($profit_sharing_add_receiver_data);
                            }
                        }
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        $WeixinStoreObj->update($wx_store_update);
                    } else {
                        $gets2 = StorePayWay::where('store_id', $store_id)->where('ways_source', 'weixin')->get();
                        $ways = StorePayWay::where('store_id', $store_id)->where('ways_type', 4000)->first();
                        $count = count($gets2);

                        $rate = 0.6;
                        $store = Store::where('store_id', $store_id)
                            ->select('user_id')
                            ->first();
                        if (!$store) {
                            return json_encode([
                                'status' => 2,
                                'msg' => '门店不存在'
                            ]);
                        }

                        $UserRate = UserRate::where('user_id', $store->user_id)
                            ->where('ways_type', $ways_type)
                            ->first();
                        if ($UserRate) {
                            $rate = $UserRate->rate;
                        }
                        $data1 = [
                            'store_id' => $store_id,
                            'ways_type' => 4000,
                            'ways_source' => 'weixin',
                            'ways_desc' => '微信支付a',
                            'sort' => ($count + 1),
                            'status' => 1,
                            'status_desc' => '成功',
                            'rate' => $rate,
                            'settlement_type' => 'T1',
                            'company' => 'weixina',
                        ];
                        if ($ways) {
                            $ways->update($data1);
                        } else {
                            StorePayWay::create($data1);
                        }

                        $wx_store_create = [
                            'config_id' => $config_id,
                            'store_id' => $store_id,
                            'status' => 1,
                            'status_desc' => '成功',
                            'wx_sub_merchant_id' => $wx_sub_merchant_id,
                            'wx_shop_id' => $wx_shop_id,
                        ];
                        if ($wxSharingOpen) {
                            $wx_store_create['is_profit_sharing'] = $wxSharingOpen;
                            if ($wxSharingType) $wx_store_create['wx_sharing_type'] = $wxSharingType;
                            if ($wxSharingTccount) $wx_store_create['wx_sharing_account'] = $wxSharingTccount;
                            if ($wxSharingName) $wx_store_create['wx_sharing_name'] = $wxSharingName;
                            if ($wxSharingRelationType) $wx_store_create['wx_sharing_relation_type'] = $wxSharingRelationType;
                            if ($wxSharingRate) $wx_store_create['wx_sharing_rate'] = $wxSharingRate;
                        }
                        $result = WeiXinaStore::create($wx_store_create);
                        if ($result) {
                            //添加分账接收方
                            $config = new WeixinConfigController();
                            $options = $config->weixina_config($config_id);

                            $profit_sharing_add_receiver_data = [
                                'store_id' => $store_id,
                                'options' => $options,
                                'wx_sub_merchant_id' => $wx_sub_merchant_id,
                                'ways_type' => $ways_type
                            ];
                            $wx_pay_obj = new WeixinPayController();
                            $res = $wx_pay_obj->profit_sharing_add_receiver($profit_sharing_add_receiver_data);
                        }
                    }
                }
            }

            //富友
            if ($ways_type == "11001" || $ways_type == "11002") {
                $FuiouStore = FuiouStore::where('store_id', $store_id)
                    ->first();
                if (!$FuiouStore) {
                    //看下是否有修改权限
                    $hasPermission = $user->hasPermissionTo('富友商户号添加');
                    if (!$hasPermission) {
                        return json_encode(['status' => 2, 'message' => '没有权限']);
                    }
                } else {
                    //看下是否有添加权限
                    $hasPermission = $user->hasPermissionTo('富友商户号修改');
                    if (!$hasPermission) {
                        return json_encode(['status' => 2, 'message' => '没有权限修改']);
                    }
                }

                if ($MerchantId == '') {
                    if ($FuiouStore) {
                        return json_encode([
                            'status' => 1,
                            'data' => [
                                'MerchantId' => $FuiouStore->mchnt_cd
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    if ($FuiouStore) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        FuiouStore::where('store_id', $store_id)
                            ->update([
                                'store_id' => $store_id,
                                'config_id' => $config_id,
                                'mchnt_cd' => $MerchantId,
                            ]);
                    } else {
                        FuiouStore::create([
                            'store_id' => $store_id,
                            'config_id' => $config_id,
                            'mchnt_cd' => $MerchantId,
                        ]);
                    }
                }


                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)
                    ->where('ways_type', 11001) //目前是一样的直接读取支付宝就行
                    ->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => 2,
                        'message' => '请联系代理商开启此通道',
                    ]);
                }

                $rate = $UserRate->store_all_rate;
                //读取商户的费率
                $StorePayWay = StorePayWay::where('ways_type', $ways_type)
                    ->where('store_id', $store_id)
                    ->select('rate')
                    ->first();
                if ($StorePayWay) {
                    $rate = $StorePayWay->rate;
                } else {
                    return json_encode([
                        'status' => 2,
                        'message' => '商户通道费率未设置',
                    ]);
                }

                //默认支付通道未注册成功
                // $data['store_id'] = $store_id;
                // $data['rate'] = $rate;
                // $data['status'] = $pay_status;
                // $data['status_desc'] = $pay_status_desc;
                // $data['company'] = 'fuiou';
                // $return = $this->send_ways_data($data);
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'fuiou')->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');

                return $return;
            }

            //随行付
            if ($ways_type == "13001" || $ways_type == "13002" || $ways_type == "13004" ||  $ways_type == "13007" ||  $ways_type == "13008") {
                $VbillStore = VbillStore::where('store_id', $store_id)->first();

                if (!$VbillStore) {
                    //看下是否有修改权限
                    $hasPermission = $user->hasPermissionTo('随行付商户号添加');
                    if (!$hasPermission) {
                        return json_encode(['status' => 2, 'message' => '没有权限']);
                    }
                } else {
                    //看下是否有添加权限
                    $hasPermission = $user->hasPermissionTo('随行付商户号修改');
                    if (!$hasPermission) {
                        return json_encode(['status' => 2, 'message' => '没有权限修改']);
                    }
                }
                if ($MerchantId == '') {
                    if ($VbillStore) {
                        return json_encode([
                            'status' => 1,
                            'data' => [
                                'MerchantId' => $VbillStore->mno,
                                'wx_channel_appid' => $VbillStore->wx_channel_appid,
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    $config = new VbillConfigController();
                    $vbill_config = $config->vbill_config($config_id);
                    if (!$vbill_config) {
                        return json_encode([
                            'status' => 2,
                            'message' => '随行付配置不存在请检查配置'
                        ]);
                    }

                    //查询状态
                    if ($VbillStore && $VbillStore->applicationId) {
                        $obj = new \App\Api\Controllers\Vbill\BaseController();
                        $url = $obj->open_query_store_url;
                        $data = [
                            'orgId' => $vbill_config->orgId,
                            'reqId' => time(),
                            'version' => '1.0',
                            'timestamp' => date('Ymdhis', time()),
                            'signType' => 'RSA',
                            'reqData' => [
                                'applicationId' => $VbillStore->applicationId,
                            ],
                        ];

                        $open_query_store = $obj->execute($data, $url, $vbill_config->privateKey, $vbill_config->sxfpublic);
                        if ($open_query_store['data']['code'] == "0000" && $open_query_store['data']['respData']['bizCode'] == "0000") {
                            if (isset($open_query_store['data']['respData']['taskStatus'])) {
                                //审核失败
                                if ($open_query_store['data']['respData']['taskStatus'] == "2" || $open_query_store['data']['respData']['taskStatus'] == "3") {
                                    $data_up = [
                                        'status' => 3,
                                        'status_desc' => $open_query_store['data']['respData']['suggestion'],
                                    ];
                                    StorePayWay::where('store_id', $store_id)
                                        ->where('company', 'vbill')
                                        ->update($data_up);

                                    $in_data = [
                                        'taskStatus' => $open_query_store['data']['respData']['taskStatus'],
                                        'wx_channel_appid' => ($ways_type == '13007') ? $xcx_channel_appid : $wx_channel_appid
                                    ];
                                    VbillStore::where('store_id', $store_id)->update($in_data);

                                    return json_encode([
                                        'status' => 2,
                                        'message' => $open_query_store['data']['respData']['suggestion'],
                                    ]);
                                }
                            }
                        }
                    }

                    //商户信息查询接口
                    $obj = new \App\Api\Controllers\Vbill\BaseController();
                    $url = $obj->store_query_info;
                    $data = [
                        'orgId' => $vbill_config->orgId,
                        'reqId' => time(),
                        'version' => '1.0',
                        'timestamp' => date('Ymdhis', time()),
                        'signType' => 'RSA',
                        'reqData' => [
                            'mno' => $MerchantId,
                        ],
                    ];

                    $re = $obj->execute($data, $url, $vbill_config->privateKey, $vbill_config->sxfpublic);
                    if ($re['data']['code'] == "0000" && $re['data']['respData']['bizCode'] != "0000") {
                        return json_encode([
                            'status' => 2,
                            'message' => $re['data']['respData']['bizMsg']
                        ]);
                    }

                    //读取商户的费率
                    $StorePayWay = StorePayWay::where('ways_type', $ways_type)
                        ->where('store_id', $store_id)
                        ->select('rate')
                        ->first();
                    if ($StorePayWay) {
                        $rate = $StorePayWay->rate;
                    } else {
                        return json_encode([
                            'status' => 2,
                            'message' => '商户通道费率未设置',
                        ]);
                    }

                    //门店不一致无法保存
//                    if ($re['data']['respData']['mecDisNm'] != $store_name) {
//                        return json_encode([
//                            'status' => 2,
//                            'message' => '门店名称不一致、无法保存'
//                        ]);
//                    }

                    //费率不一致无法保存
                    if ($re['data']['respData']['qrcodeList'][0]['rate'] != $rate) {
                        return json_encode([
                            'status' => 2,
                            'message' => '费率设置不一致、无法保存-' . $re['data']['respData']['qrcodeList'][0]['rate']
                        ]);
                    }

                    $childNo = "";
                    foreach ($re['data']['respData']['repoInfo'] as $k => $v) {
                        if ($v['childNoType'] == "WX") {
                            $childNo = $v['childNo'];
                        } else {
                            continue;
                        }
                    }

                    //报备微信授权目录
                    $obj = new \App\Api\Controllers\Vbill\BaseController();
                    $url = $obj->weixin_config;
                    $data = [
                        'orgId' => $vbill_config->orgId,
                        'reqId' => time(),
                        'version' => '1.0',
                        'timestamp' => date('Ymdhis', time()),
                        'signType' => 'RSA',
                        'reqData' => [
                            'mno' => $MerchantId,
                            'subMchId' => $childNo,
                            'type' => '03',
                            'jsapiPath' => $weixin_path ? $weixin_path : env('APP_URL') . '/api/vbill/weixin/'
                        ]
                    ];
                    $abc = $obj->execute($data, $url, $vbill_config->privateKey, $vbill_config->sxfpublic);
//                    Log::info('随行付-强开-报备微信授权目录结果');
//                    Log::info($abc);

                    $insert_data = [
                        'store_id' => $store_id,
                        'config_id' => $config_id,
                        'mno' => $MerchantId,
                        'wx_channel_appid' => ($ways_type == '13007') ? $xcx_channel_appid : $wx_channel_appid
                    ];

                    if ($VbillStore) {
                        if ($VbillStore->applicationId == "") {
                            $insert_data['taskStatus'] = 1;
                        }
                    } else {
                        $insert_data['taskStatus'] = 1;
                    }

                    if ($VbillStore) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        VbillStore::where('store_id', $store_id)
                            ->update($insert_data);
                    } else {
                        VbillStore::create($insert_data);
                    }
                }


                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)
                    ->where('ways_type', 13001)//目前是一样的直接读取支付宝就行
                    ->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => 2,
                        'message' => '请联系代理商开启此通道',
                    ]);
                }

                //默认支付通道未注册成功
                // $data['store_id'] = $store_id;
                // $data['rate'] = $rate;
                // $data['status'] = $pay_status;
                // $data['status_desc'] = $pay_status_desc;
                // $data['company'] = 'vbill';
                // $return = $this->send_ways_data($data);
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'vbill')->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');

                return $return;
            }

            //随行付A
            if ($ways_type == "19001" || $ways_type == "19002" || $ways_type == "19004" || $ways_type == "19007" || $ways_type == "19008") {
                $VbillStore = VbillaStore::where('store_id', $store_id)->first();
                if (!$VbillStore) {
                    //看下是否有修改权限
                    $hasPermission = $user->hasPermissionTo('随行付A商户号添加');

                    if (!$hasPermission) {
                        return json_encode(['status' => 2, 'message' => '没有权限']);
                    }
                } else {
                    //看下是否有添加权限
                    $hasPermission = $user->hasPermissionTo('随行付A商户号修改');
                    if (!$hasPermission) {
                        return json_encode(['status' => 2, 'message' => '没有权限修改']);
                    }
                }
                if ($MerchantId == '') {
                    if ($VbillStore) {
                        return json_encode([
                            'status' => 1,
                            'data' => [
                                'MerchantId' => $VbillStore->mno,
                                'wx_channel_appid' => $VbillStore->wx_channel_appid,
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {


                    $insert_data = [
                        'store_id' => $store_id,
                        'config_id' => $config_id,
                        'mno' => $MerchantId,
                        'wx_channel_appid' => ($ways_type == '19007') ? $xcx_channel_appid : $wx_channel_appid
                    ];

                    if ($VbillStore) {
                        if ($VbillStore->applicationId == "") {
                            $insert_data['taskStatus'] = 1;
                        }
                    } else {
                        $insert_data['taskStatus'] = 1;
                    }

                    if ($VbillStore) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        VbillaStore::where('store_id', $store_id)
                            ->update($insert_data);
                    } else {
                        VbillaStore::create($insert_data);
                    }
                }

                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)
                    ->where('ways_type', $ways_type) //目前是一样的直接读取支付宝就行
                    ->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => 2,
                        'message' => '请联系代理商开启此通道',
                    ]);
                }

                //默认支付通道未注册成功
                // $data['store_id'] = $store_id;
                // $data['rate'] = $rate;
                // $data['status'] = $pay_status;
                // $data['status_desc'] = $pay_status_desc;
                // $data['company'] = 'vbilla';
                // $return = $this->send_ways_data($data);
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'vbilla')->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');


                return $return;
            }

            //京东
            if (5999 < $ways_type && $ways_type < 6999) {
                $JdStore = JdStore::where('store_id', $store_id)
                    ->first();
                if (!$JdStore) {
                    //看下是否有修改权限
                    $hasPermission = $user->hasPermissionTo('京东商户号添加');
                    if (!$hasPermission) {
                        return json_encode(['status' => 2, 'message' => '没有权限']);
                    }
                } else {
                    //看下是否有添加权限
                    $hasPermission = $user->hasPermissionTo('京东商户号修改');
                    if (!$hasPermission) {
                        return json_encode(['status' => 2, 'message' => '没有权限修改']);
                    }
                }
                if ($merchant_no == '') {
                    if ($JdStore) {
                        return json_encode([
                            'status' => 1,
                            'data' => $JdStore
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    $config = new JdConfigController();
                    $jd_config = $config->jd_config($config_id);
                    //更新密钥
                    $store_keys = [
                        'request_url' => 'https://psi.jd.com/merchant/status/queryMerchantKeys',
                        'agentNo' => $jd_config->agentNo,
                        'merchantNo' => $merchant_no,
                        'serialNo' => "" . time() . "",
                        'store_md_key' => $jd_config->store_md_key,
                        'store_des_key' => $jd_config->store_des_key,
                    ];

                    $OBJ = new \App\Api\Controllers\Jd\StoreController();
                    $re = $OBJ->store_keys($store_keys);
                    if ($re['code'] == "0000") {
                        $des_key = $re['data']['desKey'];
                        $md_key = $re['data']['mdKey'];
                    }

                    if ($JdStore) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        JdStore::where('store_id', $store_id)
                            ->update([
                                'config_id' => $config_id,
                                'store_id' => $store_id,
                                'merchant_no' => $merchant_no,
                                'md_key' => $md_key,
                                'des_key' => $des_key,
                            ]);
                    } else {
                        JdStore::create([
                            'config_id' => $config_id,
                            'store_id' => $store_id,
                            'merchant_no' => $merchant_no,
                            'md_key' => $md_key,
                            'des_key' => $des_key,
                            'store_true' => 1,
                            'pay_true' => 1
                        ]);
                    }
                }

                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)
                    ->where('ways_type', 6001)//目前是一样的直接读取支付宝就行
                    ->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => 2,
                        'message' => '请联系代理商开启此通道',
                    ]);
                }

                $rate = $UserRate->store_all_rate;

                //读取商户的费率
                $StorePayWay = StorePayWay::where('ways_type', $ways_type)
                    ->where('store_id', $store_id)
                    ->select('rate')
                    ->first();
                if ($StorePayWay) {
                    $rate = $StorePayWay->rate;
                } else {
                    return json_encode([
                        'status' => 2,
                        'message' => '商户通道费率未设置',
                    ]);
                }

                //默认支付通道未注册成功
                // $data['store_id'] = $store_id;
                // $data['rate'] = $rate;
                // $data['status'] = $pay_status;
                // $data['status_desc'] = $pay_status_desc;
                // $data['company'] = 'jdjr';
                // $return = $this->send_ways_data($data);
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'jdjr')->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');

                return $return;
            }

            //联拓富
            if (9999 < $ways_type && $ways_type < 10999) {
                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)
                    ->where('ways_type', 10001)//目前是一样的直接读取支付宝就行
                    ->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => 2,
                        'message' => '请联系代理商开启此通道',
                    ]);
                }

                $rate = $UserRate->store_all_rate;

                $LtfStore = LtfStore::where('store_id', $store_id)
                    ->first();
                if (!$LtfStore) {
                    //看下是否有修改权限
                    $hasPermission = $user->hasPermissionTo('联拓富商户号添加');

                    if (!$hasPermission) {
                        return json_encode(['status' => 2, 'message' => '没有权限']);
                    }
                } else {
                    //看下是否有添加权限
                    $hasPermission = $user->hasPermissionTo('联拓富商户号修改');
                    if (!$hasPermission) {
                        return json_encode(['status' => 2, 'message' => '没有权限修改']);
                    }
                }

                if ($merchantCode == '') {
                    if ($LtfStore) {
                        return json_encode([
                            'status' => 1,
                            'data' => $LtfStore
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    if ($LtfStore) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        LtfStore::where('store_id', $store_id)
                            ->update([
                                'config_id' => $config_id,
                                'store_id' => $store_id,
                                'appId' => $appId,
                                'md_key' => $ltf_md_key,
                                'merchantCode' => $merchantCode
                            ]);


                    } else {
                        LtfStore::create([
                            'config_id' => $config_id,
                            'store_id' => $store_id,
                            'appId' => $appId,
                            'md_key' => $ltf_md_key,
                            'merchantCode' => $merchantCode
                        ]);
                    }
                }

                //读取商户的费率
                $StorePayWay = StorePayWay::where('ways_type', $ways_type)
                    ->where('store_id', $store_id)
                    ->select('rate')
                    ->first();
                if ($StorePayWay) {
                    $rate = $StorePayWay->rate;
                }

                //默认支付通道未注册成功
                // $data['store_id'] = $store_id;
                // $data['rate'] = $rate;
                // $data['status'] = $pay_status;
                // $data['status_desc'] = $pay_status_desc;
                // $data['company'] = 'ltf';
                // $return = $this->send_ways_data($data);
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'ltf')->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');

                return $return;
            }

            //快钱支付
            if (2999 < $ways_type && $ways_type < 3999) {
                $MyBankStore = MyBankStore::where('OutMerchantId', $store_id)->select('MerchantId')->first();
                if (!$MyBankStore) {
                    //看下是否有修改权限
                    $hasPermission = $user->hasPermissionTo('快钱商户号添加');

                    if (!$hasPermission) {
                        return json_encode(['status' => 2, 'message' => '没有权限']);
                    }
                } else {
                    //看下是否有添加权限
                    $hasPermission = $user->hasPermissionTo('快钱商户号修改');
                    if (!$hasPermission) {
                        return json_encode(['status' => 2, 'message' => '没有权限修改']);
                    }
                }

                if ($MerchantId == '') {
                    if ($MyBankStore) {
                        return json_encode([
                            'status' => 1,
                            'data' => $MyBankStore
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                }

                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)
                    ->where('company', 'mybank')//目前是一样的直接读取支付宝就行
                    ->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => 2,
                        'message' => '请联系代理商开启此通道',
                    ]);
                }

                $rate = $UserRate->store_all_rate;

                //读取商户的费率
                $StorePayWay = StorePayWay::where('ways_type', $ways_type)
                    ->where('store_id', $store_id)
                    ->select('rate')
                    ->first();
                if ($StorePayWay) {
                    $rate = $StorePayWay->rate;
                } else {
                    return json_encode([
                        'status' => 2,
                        'message' => '商户通道费率未设置',
                    ]);
                }

                //查询商户资料
                //商户信息查询接口
                $aop = new \App\Api\Controllers\MyBank\BaseController();
                $ao = $aop->aop();
                $ao->url = $aop->MY_BANK_request2;
                $ao->Function = "ant.mybank.merchantprod.merchant.query";

                $data = [
                    'MerchantId' => $MerchantId,
                ];
                $re = $ao->Request($data);
                if ($re['status'] == 0) {
                    return json_encode([
                        'status' => 2,
                        'message' => $re['message'],
                    ]);
                }
                $body = $re['data']['document']['response']['body'];

                //这个地方代表需要排查
                if ($body['RespInfo']['ResultStatus'] != 'S') {
                    return [
                        'status' => 2,
                        'message' => $body['RespInfo']['ResultMsg']
                    ];
                }
                $FeeParamList = base64_decode($re['data']['document']['response']['body']['FeeParamList']);

                $FeeParamList = json_decode($FeeParamList, true);
                $FeeValue = "";
                foreach ($FeeParamList as $k => $v) {
                    if ($v['FeeType'] == "02") {
                        $FeeValue = $v['FeeValue'];
                        break;
                    } else {
                        continue;
                    }
                }
                if ($rate / 100 != $FeeValue) {
                    return json_encode([
                        'status' => 2,
                        'message' => '请将通道费率设置为' . $FeeValue * 100,
                    ]);
                }

                $in = [
                    'status' => '1',
                    'OrderNo' => '',
                    'config_id' => $config_id,
                    'smid' => "",
                    'RateVersion' => '',
                    'OutMerchantId' => $store_id,
                    'MerchantId' => $MerchantId,
                    'MerchantName' => $store_name,
                    'MerchantType' => '',
                    'DealType' => '',
                    'SupportPrepayment' => '',
                    'SettleMode' => '',
                    'Mcc' => '',
                    'MerchantDetail' => '',
                    'TradeTypeList' => '',
                    'PrincipalCertType' => '',
                    'PayChannelList' => '',
                    'DeniedPayToolList' => '',
                    'FeeParamList' => '',
                    'BankCardParam' => '',
                    'SupportStage' => '',
                    'PartnerType' => '',
                    'wx_Path' => url('/api/mybank/weixin/'),
                    'wx_AppId' => '',
                    'wx_Secret' => '',
                    'wx_SubscribeAppId' => '',
                    'is_yulibao' => '',
                ];

                if ($MyBankStore) {
                    $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                        "user_id"=>$token->user_id
                    ]);
                    if($num>0){
                        return json_encode([
                            'status' => 2,
                            'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                        ]);
                    }
                    MyBankStore::where('OutMerchantId', $store_id)
                        ->update($in);
                } else {
                    MyBankStore::create($in);
                }

                //微信子商户支付配置接口
                try {
                    $ao->url = $aop->MY_BANK_request2;
                    $ao->Function = "ant.mybank.merchantprod.merchant.addMerchantConfig";
                    $data = [
                        'MerchantId' => $MerchantId,
                        'Path' => $weixin_path ? $weixin_path :env('APP_URL') . '/api/mybank/weixin/',
                        'OutTradeNo' => date('YmdHis') . time() . rand(10000, 99999),
                    ];
                    $re = $ao->Request($data);
                } catch (\Exception $exception) {

                }

                //默认支付通道未注册成功
                // $data['store_id'] = $store_id;
                // $data['rate'] = $rate;
                // $data['status'] = $pay_status;
                // $data['status_desc'] = $pay_status_desc;
                // $data['company'] = 'mybank';
                // $return = $this->send_ways_data($data);
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'mybank')->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');

                return $return;
            }

            //新大陆
            if (7999 < $ways_type && $ways_type < 8999) {
                $NewLandStore = NewLandStore::where('store_id', $store_id)
                    //->where('nl_mercId', $nl_mercId)
                    ->first();
                if (!$NewLandStore) {
                    //看下是否有修改权限
                    $hasPermission = $user->hasPermissionTo('新大陆商户号添加');

                    if (!$hasPermission) {
                        return json_encode(['status' => 2, 'message' => '没有权限']);
                    }
                } else {
                    //看下是否有添加权限
                    $hasPermission = $user->hasPermissionTo('新大陆商户号修改');
                    if (!$hasPermission) {
                        return json_encode(['status' => 2, 'message' => '没有权限修改']);
                    }
                }

                if ($nl_mercId == '') {
                    if ($NewLandStore) {
                        return json_encode([
                            'status' => 1,
                            'data' => $NewLandStore
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    //修改先查询信息
                    $config_obj = new NewLandConfigController();
                    $config = $config_obj->new_land_config($config_id);
                    $aop = new \App\Common\XingPOS\Aop();
                    $aop->key = $config->nl_key;
                    $aop->version = 'V1.0.1';
                    $aop->org_no = $config->org_no;
                    $aop->url = 'https://gateway.starpos.com.cn/emercapp';//测试地址

                    $sign_data = [
                        'mercId' => $nl_mercId,
                    ];
                    $is_qd = $config->is_qd;
                    $request_obj = new  \App\Common\XingPOS\Request\XingStoreShangHuChaXun();
                    $request_obj->setBizContent($sign_data);
                    $return = $aop->executeStore($request_obj);
                    //不成功
                    if ($return['msg_cd'] != '000000') {
                        return json_encode([
                            'status' => 2,
                            'message' => $return['msg_dat'],
                        ]);
                    }

                    //直接通过
                    if ($return['check_flag'] == 1) {
                        $pay_status = 1;
                        $pay_status_desc = '开通成功';
                        $nl_key = $return['key'];
                        // $trmNo = $return['REC'][0]['trmNo'];
                        $nl_stoe_id = $return['REC'][0]['stoe_id'];
                    } else {
                        return json_encode([
                            'status' => 2,
                            'message' => '商户未开通成功',
                        ]);
                    }
                    $in_data = [
                        'config_id' => $config_id,
                        'store_id' => $store_id,
                        'nl_key' => $nl_key,
                        'trmNo' => $trmNo,
                        'nl_mercId' => $nl_mercId,
                        'nl_stoe_id' => $nl_stoe_id,
                        'jj_status' => 1,
                        'img_status' => 1,
                        'tj_status' => 1,
                        'check_flag' => 1,
                        'check_qm' => 1,
                        'is_qd' => $is_qd,
                    ];
                    if ($NewLandStore) {

                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        NewLandStore::where('store_id', $store_id)
                            ->where('nl_mercId', $nl_mercId)
                            ->update($in_data);


                    } else {
                        NewLandStore::create($in_data);
                    }
                }

                if ($wx_sub_appid == '') {
                    if ($NewLandStore) {
                        return json_encode([
                            'status' => '1',
                            'data' => $NewLandStore
                        ]);
                    } else {
                        return json_encode([
                            'status' => '1',
                            'data' => []
                        ]);
                    }
                } else {
                    $NewLandStore->update([
                        'wx_sub_appid' => $wx_sub_appid
                    ]);
                    $NewLandStore->save();
                }

                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)
                    ->where('ways_type', 8001)//目前是一样的直接读取支付宝就行
                    ->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => 2,
                        'message' => '请联系代理商开启此通道',
                    ]);
                }

                $rate = $UserRate->store_all_rate;
                //读取商户的费率
                $StorePayWay = StorePayWay::where('ways_type', $ways_type)
                    ->where('store_id', $store_id)
                    ->select('rate')
                    ->first();
                if ($StorePayWay) {
                    $rate = $StorePayWay->rate;
                } else {
                    return json_encode([
                        'status' => 2,
                        'message' => '商户通道费率未设置',
                    ]);
                }

                //默认支付通道未注册成功
                // $data['store_id'] = $store_id;
                // $data['rate'] = $rate;
                // $data['status'] = $pay_status;
                // $data['status_desc'] = $pay_status_desc;
                // $data['company'] = 'newland';
                // $return = $this->send_ways_data($data);
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'newland')->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');

                return $return;
            }

            //和融通
            if (8999 < $ways_type && $ways_type < 9999) {
                $JdStore = HStore::where('store_id', $store_id)
                    ->first();
                if (!$JdStore) {
                    //看下是否有修改权限
                    $hasPermission = $user->hasPermissionTo('和融通商户号添加');
                    if (!$hasPermission) {
                        return json_encode(['status' => 2, 'message' => '没有权限']);
                    }
                } else {
                    //看下是否有添加权限
                    $hasPermission = $user->hasPermissionTo('和融通商户号修改');
                    if (!$hasPermission) {
                        return json_encode(['status' => 2, 'message' => '没有权限修改']);
                    }
                }

                if ($h_mid == '') {
                    if ($JdStore) {
                        return json_encode([
                            'status' => 1,
                            'data' => $JdStore
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    if ($JdStore) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        HStore::where('store_id', $store_id)
                            ->update([
                                'config_id' => $config_id,
                                'store_id' => $store_id,
                                'h_mid' => $h_mid,
                                'h_status' => $pay_status,
                                'h_status_desc' => $pay_status_desc,
                            ]);
                    } else {
                        HStore::create([
                            'config_id' => $config_id,
                            'store_id' => $store_id,
                            'h_mid' => $h_mid,
                            'h_status' => $pay_status,
                            'h_status_desc' => $pay_status_desc,
                        ]);
                    }
                }

                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)
                    ->where('ways_type', 9001) //目前是一样的直接读取支付宝就行
                    ->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => 2,
                        'message' => '请联系代理商开启此通道',
                    ]);
                }

                $rate = $UserRate->store_all_rate;

                //读取商户的费率
                $StorePayWay = StorePayWay::where('ways_type', $ways_type)
                    ->where('store_id', $store_id)
                    ->select('rate')
                    ->first();
                if ($StorePayWay) {
                    $rate = $StorePayWay->rate;
                } else {
                    return json_encode([
                        'status' => 2,
                        'message' => '商户通道费率未设置',
                    ]);
                }

                //默认支付通道未注册成功
                // $data['store_id'] = $store_id;
                // $data['rate'] = $rate;
                // $data['status'] = $pay_status;
                // $data['status_desc'] = $pay_status_desc;
                // $data['company'] = 'herongtong';
                // $return = $this->send_ways_data($data);
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'herongtong')->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');

                return $return;
            }

            //哆啦宝
            if (14999 < $ways_type && $ways_type < 15999) {
                $DlbStore = DlbStore::where('store_id', $store_id)->first();
                if (!$DlbStore) {
                    //看下是否有修改权限
                    $hasPermission = $user->hasPermissionTo('哆啦宝商户号添加');
                    if (!$hasPermission) {
                        return json_encode(['status' => 2, 'message' => '没有权限']);
                    }
                } else {
                    //看下是否有添加权限
                    $hasPermission = $user->hasPermissionTo('哆啦宝商户号修改');
                    if (!$hasPermission) {
                        return json_encode(['status' => 2, 'message' => '没有权限修改']);
                    }
                }

                if ($getdlbdata == 1) {
                    if ($DlbStore) {
                        return json_encode([
                            'status' => 1,
                            'data' => [
                                'mch_num' => $DlbStore->mch_num,
                                'shop_num' => $DlbStore->shop_num,
                                'machine_num' => $DlbStore->machine_num
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'message' => '没有配置',
                            'data' => []
                        ]);
                    }
                } else {
                    $check_data = [
                        'mch_num' => '商户编号',
                        'shop_num' => '门店编号'
                    ];
                    $check = $this->check_required($request->except(['token']), $check_data);
                    if ($check) {
                        return json_encode([
                            'status' => 2,
                            'message' => $check
                        ]);
                    }
                    if ($DlbStore) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        DlbStore::where('store_id', $store_id)
                            ->update([
                                'store_id' => $store_id,
                                'config_id' => $config_id,
                                'mch_num' => $mch_num,
                                'shop_num' => $shop_num,
                                'machine_num' => $machine_num
                            ]);


                    } else {
                        DlbStore::create([
                            'store_id' => $store_id,
                            'config_id' => $config_id,
                            'mch_num' => $mch_num,
                            'shop_num' => $shop_num,
                            'machine_num' => $machine_num
                        ]);
                    }
                }

                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)
                    ->where('ways_type', 15001) //目前是一样的直接读取支付宝就行
                    ->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => 2,
                        'message' => '请联系代理商开启此通道',
                    ]);
                }

                $rate = $UserRate->store_all_rate;

                //读取商户的费率
                $StorePayWay = StorePayWay::where('ways_type', $ways_type)
                    ->where('store_id', $store_id)
                    ->select('rate')
                    ->first();
                if ($StorePayWay) {
                    $rate = $StorePayWay->rate;
                } else {
                    return json_encode([
                        'status' => 2,
                        'message' => '商户通道费率未设置',
                    ]);
                }

                //默认支付通道未注册成功
                // $data['store_id'] = $store_id;
                // $data['rate'] = $rate;
                // $data['status'] = $pay_status;
                // $data['status_desc'] = $pay_status_desc;
                // $data['company'] = 'dlb';
                // $return = $this->send_ways_data($data);
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'dlb')->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');


                return $return;
            }

            //传化
            if (11999 < $ways_type && $ways_type < 12999) {
                $TfStore = TfStore::where('store_id', $store_id)->select('sub_mch_id', 'qd')->first();
                if (!$TfStore) {

                    //看下是否有修改权限
                    $hasPermission = $user->hasPermissionTo('TF商户号添加');
                    if (!$hasPermission) {
                        return json_encode(['status' => 2, 'message' => '没有权限']);
                    }
                } else {
                    //看下是否有添加权限
                    $hasPermission = $user->hasPermissionTo('TF商户号修改');
                    if (!$hasPermission) {
                        return json_encode(['status' => 2, 'message' => '没有权限修改']);
                    }
                }


                if ($MerchantId == '') {
                    if ($TfStore) {
                        $config = new TfConfigController();
                        $tf_config = $config->tf_config($config_id, $TfStore->qd);

                        if (!$tf_config) {
                            return json_encode([
                                'status' => 2,
                                'message' => '传化配置不存在请检查配置'
                            ]);
                        }

                        $obj = new \App\Api\Controllers\Tfpay\BaseController();
                        $obj->mch_id = $tf_config->mch_id;
                        $obj->pub_key = $tf_config->pub_key;
                        $obj->pri_key = $tf_config->pri_key;
                        $method = '/openapi/merchant/open-query';

                        $channel = '0';
                        if ($ways_type == "12002") {
                            $channel = '1';
                        };
                        //传化支付宝报备
                        $post_data = [
                            'sub_mch_id' => $TfStore->sub_mch_id,
                            'channel' => $channel,
                        ];

                        $re1 = $obj->api($post_data, $method, false);
                        $channel_mch_id = isset($re1['data']['channel_mch_id']) ? $re1['data']['channel_mch_id'] : "";

                        return json_encode([
                            'status' => 1,
                            'data' => [
                                'MerchantId' => $TfStore->sub_mch_id,
                                'qd' => $TfStore->qd,
                                'channel_mch_id' => $channel_mch_id,
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                }

                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)
                    ->where('company', 'tfpay')//目前是一样的直接读取支付宝就行
                    ->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => 2,
                        'message' => '请联系代理商开启此通道',
                    ]);
                }

                $rate = $UserRate->store_all_rate;
                //读取商户的费率
                $StorePayWay = StorePayWay::where('ways_type', $ways_type)
                    ->where('store_id', $store_id)
                    ->select('rate')
                    ->first();
                if ($StorePayWay) {
                    $rate = $StorePayWay->rate;
                } else {
                    return json_encode([
                        'status' => 2,
                        'message' => '商户通道费率未设置',
                    ]);
                }

                //传化报备
                $config = new TfConfigController();
                $tf_config = $config->tf_config($config_id, $qd);

                if (!$tf_config) {
                    return json_encode([
                        'status' => 2,
                        'message' => '传化配置不存在请检查配置'
                    ]);
                }
                $obj = new \App\Api\Controllers\Tfpay\BaseController();
                $obj->mch_id = $tf_config->mch_id;
                $obj->pub_key = $tf_config->pub_key;
                $obj->pri_key = $tf_config->pri_key;
                $sub_mch_id = $MerchantId;

                //签署电子合同
                $post_data = [
                    'sub_mch_id' => $sub_mch_id,
                ];

                $method = '/openapi/merchant/contract';
                $re = $obj->api($post_data, $method, false);

                //费率同步
                $rate1 = $rate * 10;
                $post_data = [
                    'sub_mch_id' => $sub_mch_id,
                    'WECHAT_POS' => $rate1,
                    'WECHAT_SCAN' => $rate1,
                    'WECHAT_MP' => $rate1,
                    'ALIPAY_POS' => $rate1,
                    'ALIPAY_SCAN' => $rate1,
                    'ALIPAY_MP' => $rate1,
                ];

                $method = '/openapi/merchant/rate';
                $re = $obj->api($post_data, $method, false);
                if ($re['code'] != 0) {
                    return json_encode([
                        'status' => 2,
                        'message' => $re['msg']
                    ]);
                }

                //新增app ID
                $post_data = [
                    'sub_mch_id' => $MerchantId,
                    'sub_appid' => $tf_config->wx_appid,
                ];
                $method = '/openapi/merchant/wechat/appid';
                $appid = $obj->api($post_data, $method, false);

                //报备支付授权目录
                $post_data = [
                    'sub_mch_id' => $MerchantId,
                    'jsapi_path' => env('APP_URL') . '/api/tfpay/weixin/',
                ];
                $method = '/openapi/merchant/wechat/jsapi-path';
                $jsapipath = $obj->api($post_data, $method, false);

                $in = [
                    'store_id' => $store_id,
                    'config_id' => $config_id,
                    'agent_id' => $tf_config->mch_id,
                    'sub_mch_id' => $MerchantId,
                    'status' => 1,
                    'qd' => $qd
                ];
                if ($TfStore) {
                    $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                        "user_id"=>$token->user_id
                    ]);
                    if($num>0){
                        return json_encode([
                            'status' => 2,
                            'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                        ]);
                    }
                    TfStore::where('store_id', $store_id)->update($in);
                } else {
                    TfStore::create($in);
                }

                //默认支付通道未注册成功
                // $data['store_id'] = $store_id;
                // $data['rate'] = $rate;
                // $data['status'] = $pay_status;
                // $data['status_desc'] = $pay_status_desc;
                // $data['company'] = 'tfpay';
                // $return = $this->send_ways_data($data);
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'tfpay')->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');

                return $return;
            }

            //工行
            if (19999 < $ways_type && $ways_type < 20999) {
                $data = LianfuStores::where('store_id', $store_id)
                    ->select('apikey as MerchantId', 'signkey', 'appid')
                    ->first();

                if ($MerchantId == '') {
                    if ($data) {
                        return json_encode([
                            'status' => 1,
                            'data' => $data
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {


                    $appid = $request->get('Icbc_area', '10000000000003233525');
                    $signkey = 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCykODcxD2oQfFg5dVkynRiuWwTtUeQEJlS4uQmgdz+Nwjr4w+HCVCk5oTy45XiuksdwvVvscCMMIThUkqnSRiGOlhf/D3XowT8l+I0rzFIvZn7exyA9ZDTI/fkY8zvrTGHsBDfk0dygLAMyZbTdskLSj+yCo9ouI5KwwclgyZdQueI/XOZ1QcD/0iiXUvPV6tc4gEnhkEAqB9cbL4DyrKTBOhrVXoCxCAIeyDYs3DhIdhmn0Zz6PUqX4fyRbgBJjpGh6Yg4Z4aBejamN4JixThe3urpGx3XYxDKfX8PWKeqayXFhcuNy42U9OwmVWEbLk9pBmooz04vGSmVYQFAoaNAgMBAAECggEAdN0CfRllrNezbJIebX9eWjo19FQAyNVEMw3Q+S6mfi1whkitIdhD0hgXzIqLzr3TaV/tlzMC6/zZCkn8F61sAnTu36rKonTLT7DrV0WNAKktvHse9XtBVB3HH879EaMMtlvRPzE5YkO8dDH+ptinzN7D/vaQUp3uahlndPJ3SjLmBl18+WZKjFyd4v4UNCoEN+SBzQckB8bukPtvPGLDtFJErCvWRCX4FjrdQML9AGz4PCesECfLRJzPe3T9dP016kJQQKNRZsjg6gLjWrcVAXPwabTkAfONUiBSjizFV6N0rki8Yg46PUpUeQ0mQ5bi1FC75qefCn7rJIemIZPP4QKBgQDMVfBiYGdpyHdPU7HV2IlOX1fgaMHHmJg8GD8SU9O9+NWUg2qE2+uJUugIVVuvdw98Gcgm6fyDPngg1W8W5BbxgJsnwNjhxsg0o7FkH3mzuUuQYb9SbsVyDmvZDCnJl7hRvwIb56oH36UwXqD9ZS+1j5mHsFKA5HuPC7MXb37ABQKBgQDftu/A3Q/uWqaJpIvkQDHmTIq1bR38OibUPmsukwLKa+qTRanx4APak8ZMMpLynf4uw8uh3tufdzFuVUTUFexYNIbjBjP42jCXu54bmDk9V5KHBiGjGpSJmCPhP6d5IN2e5li2UL+C3fOYRqZejUHJaboixC6uN/ZWFPNay9Ra6QKBgQDAO21Nr7I6U4zjBTL9opvyLkJG7Wa4dvdpOcpEXLaMN0f/Uu7oFNlutJ90D5GU7maT8tFYwrJ7ak6rXQAIwgZPpxTQfl3Lci4xT3p4XiApk33iVAMg8vMvzR9q70V00RZnWVILnJNamu0WJ0A/g20x2umuPJQxjEItne0bdBoDXQKBgFHQFefjMDNLxYGqwloLu/Pr5AIrUH7wSUjvNsVGkAu04YlQPbEt6lriFe/BwwmlC9LQWcTtukWcEmC2IECrb85mDzoxB4qnSIQE4fJXff9X6kckz+JVEyMql1yFqdpbUZENzj2GVDMoti/nDmqfoAJnfuG9+xBNePHMIZ7/0b4ZAoGAU8FjZD4v552N0rK82skYWuQNUpKZ7f/xbMcC7EEDNqMpRPBz0GfdmnFSbx3KZKHAHbiqcYnPKuIRlc6SH0USpX96Me0A9NTZQIQQr0OVE4W0in2SEIJofb2Lu6xm30ipsQSTO1cxUUS7Qjo+HsBp8R/qFxWS+wiKLagoa/EAXzA=';
                    $area = [
                        '11000000000000013347' => 'MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCrd3OW5Zs0PmPolGi7sYQuUKOB/x0ycK6vaZh7ltT7VqYlbC5Z3gXkdYvdKmqna+zv8xrxynEYie7/5RlN+sSSOXc1AIJC58OLwEh+dhjCZWqdgmIEUQgDJh8We+g37ecbrWUjGDTszaTxx2nVkJ61dT7UnTgKdjS1+qDqdXOShEHLaizHfbR0LTxA7S7BCQaQcQqPX+7a3cLVeK9FaZG3/GcaokHteIVTjYNoPhYXuuEGUqzQlM9sUb2Dkk1KqnYk6FzJqxsyCYWdNa4omJDaAMMpZf06QsvE5eNkOfUF0BU1K/13aIV/7jwNf/P9O1dbcnEQ6jzNLQgfMPhCI8LnAgMBAAECggEAWNpwmqxm7f66tSZa05Ccx4dgA5hY3++Ghj25F6pEue3f8RqLNmSYNxg2nyhA1ntV783Rk6RtUOMBZ7enkkI1pPSEcoHN/q+tJHlC182I7Quy4OZUtpnrBf62M2eIIZt1nGERPMtpeaDjLatBi7sCk1OreG/0P2+bxwWWZ4jvl+d5EhFe18FwmDsG64d+kCNH/adhVEvWg9d5Oj/1qnkf5XSDCG2dmh146hzT37Vpuc/Ne050R30tomLJexZYInebaQmPOumkxmyZ6WD7jn34T5EV/mfO0A6g8VFjNlstCH0WjPwGG8NgmgOWRQGaqgp/MMaMeudhI3kyVu0QvzYygQKBgQDn6ByIsG/e6trp+sSHwsIcgpqa2Ym2OHhKkZIe+uip+Nxc4Arm0wP4Ab0x9WBS8lw+psDyvZG23744qFIFnbbsMw4rRTiUfuXBohIB8y99p2qZB/fNl3i5cSDDUVjDUkEn9vcrujcLpavG5O37meykelY9OxJErjvRyn/AyEFjwQKBgQC9R9mhhMSyfpUq8Z3JFzbN8fOZ6xeISpu0odDep0an6PF3Vc5n9S/CbadbQG1MeP4uvwgdfJJVSVswJQrfBMOc4B9/Ds1FjD2Q18095pDgnufOGEAkWEuAA5AF8PSaCccXroThXAgU7hsbWmXQlvMOjDA4vTuhK6QupgwmmKewpwKBgDdcXLtwyzP/AsWKfLIgFzCxKPFAbzSV5HmAqM5GfZZd91UgktmplC/3UlAD0LvcaZL3QeZwxMFQRlvGKtZ2YP2f7oj73FbuuDByXTQj9rlbZwIjOc/VkhdK31RhLfxqvW0p8rV4nb1lDk3LxtgiVbCqF75ERMcqhNM/MSI3bAzBAoGBAIjsnhnCwsvg2CW9rYfXvDSVD8TLlTVpy2OLH17CIVG8KFlMgEkhzE/ZJRTU8Xw9lprYZJV4FR+PNmEImumkN6GXXBF88q+Z+Lt78SQTU8C6EzQ6xMPF/6/2Y8x4d2mrIxQVkli4S7xgVsmdxkNzvxZKAWQ6OBWqTVXVlrqg+cXHAoGBAJwG3qw50ImyXvCTQ4v1iHJ0geu7AT67M+rxSP+3NryS3N0OEJ6lHu/mbEdrEowp3yI/npgOm5CQMBx1VnEOemzINjldegk8ZGlBFUEjizh+mArkoaUQI9w11mZ5sxkSxT5y7MrzvCEm3S4qxemZdfqhmWrufcoki2JRHoriZUQN',
                        '11000000000000025799' => 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDAXEozNDECxRtiXCGHtKXiL3twqYRqewHNM/zynxKBLnDQ4OsxoI6bguFO7BYxUMGbNAhFhiNe33s3nnc3akEb1CAozE3OZ2kwKBFwOnnhTDG5ZTgLCoI4YJmcpyIQmWA48OR7WwX/QlhU0GcDn9cL4hSJWp3BleW+ih68xGA0F6twU8LerJpNMiZfWRQCNmf60ncub+ARyk/y+aVjmjVkeOf5r2U1lvDDZex46LHxRVyjhMfPQU/CpSl7xcCgUIJ303aw29uj3jQjhGX+g1EwYyYE5l8/IumuiFehTeDwxwTMHp/ayI78iEVPBHVjNpJfXtTzqnu3RFMz3Rqnn+dxAgMBAAECggEABxoBJUXKf2sueSGknUF2lFSjboPbnyC+bsh6EKz9vmYsBRIgrLrcOPKKheLiaNyt4SB5I7Pxj1mZz3JavWF/WNsqesccIkAgGoaIKkgGYuJwvYL2oDW5nAI4+ZcUA1/eNQvzq3gvTv6wiyUMgI5AnrAwiOefBlwV2MR5oQdw88tRaNpzo6qUgdpphF1IZfH0zUInxijdWhTFHnYLqEsEUWHot7PqhS/3WMzpxkLQ2ikCyV6F57/d1r/p7/ugiNUR0hm9wyuvni7FwU0E/2DyBesyJJ0GX/m2DyQzqpvyGWkqbDdycXNHNyNPCH/eK2pJ6IPianJZhqHeTA8frKbT1wKBgQD6FXAAb7zWpn+XyJpzPR0hyzkSIFAwc5sV8QrK6p+7NNWbr4IueoAy2iZusl5hU2+NkUTAgc78ZMOUaGCtCtV6TbSMxgbCr6qPB5OdaZDZgRMOu13282oVxT6T4Vk0uY7mqd+xJ4Jm9IycbWHlYCw96NBjYb6sVbygx5Yyjbk/ywKBgQDE6USFUTwIYT5lB8SGgXNXuihxPheyRNFNPW6aUVXUczl6/eAppxe+Tl85uSVqfSfDEytrhptDe76y+ictdUBYgcZ5PgCNuc7RRprIpT9YwCp4VRnXIOn4sd7d9QGPIktW29hV7QVwGDjMS0szdCcFtedht4HmcuTC/Us4U55WMwKBgQCfLVn2zShTL/Lyt3qncvTB98pBoVOpI0xJMP1/nstAqyZzS8j+sHY85+Gif+HW7PaKEzbBmV4YBEMAiVdPjR/IEwGiwMC41nsnQtlQTksmRiPNMrFiaBjT9pIYKk1PY/RzGRkKd21m/q0pO+4dw5MlbLSHLUXiB0hixebyEocg9QKBgBsj777UYIrjJF92f57ubsBztylMnqWCmLqlsf3xIfxjWVPbdC8pibtAQB3LqB2SXI2lscFjTzkw8CrpEcdRNuo8EOgbJKZkKt5rdeAb7qxD6FannCUrszQFCCvdnjZlSq8RJDNLtQkrPYDjC91usAY4eUjdqzjhMyixEtITrGpDAoGAKtVPNz+g8HIRqFUKS7HoF7UhLpH51Of7ZZJUQ11B7gacQ2AmWacevqO+ynQzwxoOLRhrv//C5gFPSDhDae63oyJVFJy93YQ2d/RdhQk+2JY1UDoF0Lo2RLKbwaL4k7Tr6MAXXeSqLsuZLZEF9UPUTbd9qpVn1EwqBHObD+SoMNY=',
                        '10000000000003233525' => 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCykODcxD2oQfFg5dVkynRiuWwTtUeQEJlS4uQmgdz+Nwjr4w+HCVCk5oTy45XiuksdwvVvscCMMIThUkqnSRiGOlhf/D3XowT8l+I0rzFIvZn7exyA9ZDTI/fkY8zvrTGHsBDfk0dygLAMyZbTdskLSj+yCo9ouI5KwwclgyZdQueI/XOZ1QcD/0iiXUvPV6tc4gEnhkEAqB9cbL4DyrKTBOhrVXoCxCAIeyDYs3DhIdhmn0Zz6PUqX4fyRbgBJjpGh6Yg4Z4aBejamN4JixThe3urpGx3XYxDKfX8PWKeqayXFhcuNy42U9OwmVWEbLk9pBmooz04vGSmVYQFAoaNAgMBAAECggEAdN0CfRllrNezbJIebX9eWjo19FQAyNVEMw3Q+S6mfi1whkitIdhD0hgXzIqLzr3TaV/tlzMC6/zZCkn8F61sAnTu36rKonTLT7DrV0WNAKktvHse9XtBVB3HH879EaMMtlvRPzE5YkO8dDH+ptinzN7D/vaQUp3uahlndPJ3SjLmBl18+WZKjFyd4v4UNCoEN+SBzQckB8bukPtvPGLDtFJErCvWRCX4FjrdQML9AGz4PCesECfLRJzPe3T9dP016kJQQKNRZsjg6gLjWrcVAXPwabTkAfONUiBSjizFV6N0rki8Yg46PUpUeQ0mQ5bi1FC75qefCn7rJIemIZPP4QKBgQDMVfBiYGdpyHdPU7HV2IlOX1fgaMHHmJg8GD8SU9O9+NWUg2qE2+uJUugIVVuvdw98Gcgm6fyDPngg1W8W5BbxgJsnwNjhxsg0o7FkH3mzuUuQYb9SbsVyDmvZDCnJl7hRvwIb56oH36UwXqD9ZS+1j5mHsFKA5HuPC7MXb37ABQKBgQDftu/A3Q/uWqaJpIvkQDHmTIq1bR38OibUPmsukwLKa+qTRanx4APak8ZMMpLynf4uw8uh3tufdzFuVUTUFexYNIbjBjP42jCXu54bmDk9V5KHBiGjGpSJmCPhP6d5IN2e5li2UL+C3fOYRqZejUHJaboixC6uN/ZWFPNay9Ra6QKBgQDAO21Nr7I6U4zjBTL9opvyLkJG7Wa4dvdpOcpEXLaMN0f/Uu7oFNlutJ90D5GU7maT8tFYwrJ7ak6rXQAIwgZPpxTQfl3Lci4xT3p4XiApk33iVAMg8vMvzR9q70V00RZnWVILnJNamu0WJ0A/g20x2umuPJQxjEItne0bdBoDXQKBgFHQFefjMDNLxYGqwloLu/Pr5AIrUH7wSUjvNsVGkAu04YlQPbEt6lriFe/BwwmlC9LQWcTtukWcEmC2IECrb85mDzoxB4qnSIQE4fJXff9X6kckz+JVEyMql1yFqdpbUZENzj2GVDMoti/nDmqfoAJnfuG9+xBNePHMIZ7/0b4ZAoGAU8FjZD4v552N0rK82skYWuQNUpKZ7f/xbMcC7EEDNqMpRPBz0GfdmnFSbx3KZKHAHbiqcYnPKuIRlc6SH0USpX96Me0A9NTZQIQQr0OVE4W0in2SEIJofb2Lu6xm30ipsQSTO1cxUUS7Qjo+HsBp8R/qFxWS+wiKLagoa/EAXzA=',
                        '10000000000003212249' => 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDWrDXv26nLfpCgZesUCrPGDXP4MXJnoZcX7IXKeMG1Rps7yXfstp+UjUFG4PlQfuDLRArD6bmigBerIjH3ikGBVCPEsojMJ+AN21UCUDzgawPrqA073eOqrTAejCVLOqJaRaCWjFyi5pFr84oKH/B1pCcYb1llBrOvlfT3juA3iNvc+IWUoIittK4V+ED+nBSHkaDux0Q2XX13VZ07ZfPik51Wv6dPTRDBPFZ73pogQDhiWB7kL7gSZyjnANSZm6V4DDxdEwUbNb6qbcW74oQUZl7ggGOOO2LOcJNl5hXtg5cLaVTaaEkjCy8FpJs4pZfjJAckoqKc6nORf+mvQezLAgMBAAECggEBAIaxRqZYjRDBOL1rNTQ+As4hYsI3RDEBkmvQW6HOYrv6sSuyHSzI6ResCWkPf16iPSlMZ/TEkVUESdV2fyNx++fJgyfHXU27Nzl6hLwO3CNzAeK6bpNBhbdxMt/3zu6Eu5koVrofF4Ceal/xTotLvvQRKvaJ5u0cZ2GkFS++1bABaHb43BSOQeU4kFuAN7nxj72VqTyADq7bOQ8fr+sjBZMhzFU1bX8uQVeSCrPnK5W+JQpNBWjiD25TZlt2y48N88eGvLuasxCxHKWTDC1J4/LOXbhGxE6S9GJb9QOS9iGCwBHUV2G+52vVPGGCqM0Y2XfGb9Czs3gVj8gAdFrWYQECgYEA4Aqb3Bnzju4pc9s93t8rtD3HNK9Hp4DqbemahWfj23vzZoxg/tORDK6PZvfHTJIUpJxIRGqz8BFfRBET2xkNgiqw9iAPlViU33aaV+ITNwZvSIcwGhXt00jh9MYsZ1bU3vsOqAAVRx7sOuDl2PTJFmbiPpZDHp3cMieV44/aV+cCgYEA9Ut7BjuoQPLHQhLHz4gr+5ZCHNg+ZaF3MfRgDf84FHpNilIHYdSdIh6CFrnFBwzQkLuXBva/XYR/5Vc9BJ9GmYGWN645iJeSC+LYGZPx27fJj9Uzns2f0iDZLqoJAu5iaD8+VA/GstuzH6Gpxr/EW+2dYNEXLBDt7sB5fH1e130CgYBw0MbFIYhEm+bhwS+5j7VpMIakH/M8BK2HpU87CCJz/kmFhQC+sPhkY24wkmqYQyMD9PJrCTQMmiLXmqXd+RKVTjwH5AMU2CM3F3kG8f9tAN9+rRxqtkooz5WAdV4ct1u/4LnznP8Z7hdUWrVC95heoHYiv/WScDlWRBq1GVYAVwKBgGDtr7J51nW7ucNBOpWmYILtEDjntlhgooIp598AD///iC51VZdx9Zmq5ormY5XymWPnzLkiveHNXuCYRXNTIQm9rEdaYD+6bSqnO+93h6XkDRD6FYjWBiZ5dni7Fg6Ddn1tKEr9ZO3+ZNzF/BmeY5BT59F/LiHqmqqygg8QV27dAoGARcbpFdTd+6ZlsWbg2uYGhXWp7rWC7exWUM5BUIccw/A+HgnjbjUvsgA4ol9J8OfeFZe1EPioMZY/Il7/flp0qXeBohhj/YFfBsym5ywogbvxyPvbvxsxIT1GoetPhFzPmn6Vr26kffLWlGSeMuATdELX6opX1icpEL268e1CTd0=',
                        '11000000000000021944' => 'MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC9BbB2bibYN5eHNdvZVpLqo7MuCBTCle67Ut7hdrcI3ygnYuo9+GwUOCNAzZOoH2mDOl/Kb+S1LGX/rJAmDP1lgNGNHJHjHqDH0KT+DwMo8cgP9Pd5i7/AdIYOfTZkFWsQH46azkI1mJyzYf7Iz3X4CK23YcSETtwskN/SPdegMEePjNhQeEfdgKSixISKOXVDG1FqBJRSfiS6E/U0JUQ5fHOSHx+oPbzO2/0kqXtXkwRgJwfYpkrrNBIxe6aMwsBZ+Kt18k0abM6RRv/R9AzM7zRR1vEHhXUmyLHEKvMVikGqikKoXlmYTv5Q6VwGtR+5N+Y+1iciCuWNW14CXeMdAgMBAAECggEATYt19z/9Q5qa2WdpB+KSZxw9N9cs3R8xTzwSH6DaOhOCn5XezwmJHUpifAVaXBB28tUR0zTQONs8BQXAJTwTAH82eJON7gT0pMSdy3RRMYzvLEys8fo48jVbgbowhCKdR4IKuMuBMgen0ZeUNMGXfefhjGfZfQX1IXtVtySTvy0aHDQeSGsOkunPShu2HpDKT0nqC0DYkiV0lF/DOOwUsm4bPosvOj/SzLwnbtkap2jKJVNSuNDgf0cBA6XXD5lLvTcK4SkAagiAW3mig9opOtJuM30j0TQ7PoLq2I2x6vs05k7OZN35S10eU+AfJX6xbCXI3bOcjVdPSCXK8KdkKQKBgQDwUeXW8ry7bBKpYYjyTsR6/Zy9NwlMKNQHPnes9yUOYEeiKWgX7XJO4d2jRzgi/EvNZVnz5RYGIxffg4aBVeMe9RPsVzUr4ZZczNBD4R4d4JKWcb3ZR8x7mhreO/SqgeCa+ir2oc8g/fRtPC/VQbDLTjwswOpJmz3x/naruBQ8+wKBgQDJWvU7Gbwq5zpt28xWNHWZBCeOm7sM3w1mfysXnTIM6121DBpc0iG+NRBJzUny+9tu/DJBQRfJrxKnit8f3lZpBVh0AtdlBo3xTRS8sDcQLsTtMRiCT/Thh11Han/OMptjhxHqxjVamgavAfZ0mxqe7TWbVQL7SENzzSepX6a0xwKBgQDOZpiPf1VnZ0+z2EhFTC9mLrHStRUwKxTHz/f7Pxy9dyWu+f8kJdR6OXUhSgbFzYjRNM4IMmYzCSr01kLMjFaP/Q+Kee/56zs4ItRwqEU0he+AdNoIZqkSM24GFtpxIzEkCd6iiqCaBmNK+llewJe2VVBLY7kVVNbkI5gKrgP7xwKBgQDCOWqzR2+qMsC5l3rS7GdC4i8F8FHNaJqUc7YtYQQWUjJhoPsM7eR+hNDhX5QlPau8sqgHKbK4aJel5N607tKLGwznWpZKbLgeOiv2lkOG6JYr9V+e02Fy6g+gA6qkS0x4BKHTxL1SV6B6z3nboIFdK5K58padrGbkHNBVIF69oQKBgQDlsMp/PtjA/sWMIz1IYBLEzxWzj7wXa5DC4OBw97FruGqRjSpQSbTHkDBoMiSSTaaAl6eRCKRu8c5WD0WYXAs5TXGsBy/sU7JWcWtjXTeIjXBn5/R1TrMzufy4lIOYNiOYG7EZtXXW0E/0uV4p6ry7JVttFL/r0/6Yv21DWSs8WQ==',
                        '11000000000000025956' => 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCn16L+z99MnOTq3cT9ZzFAnbzKIGxDzvu0P0puaK8MQF25wnasfF3/SeoGi7ZMjfsao+tawdYoBHfUTCVL7AqUcgW4OidvakidL5Ij2HZDaMtdOm+i7lu4TRebp3kEiD2q7ll/vnpTGkAXfKIj56kfKTbB1dHpy/zfLcnDnxR1srt8Sq80ugflATS3Cxb9dmL0cR1+XRu5u/Grb12hql+bPBSM5kKy4oSCynRAip90xURYNzXue/p/WlvhuzOAbIcNBnsvwpxNyNOuZKxV7bsH4Ftn1csCA53N+W+vZQzsNm5o5E1IE8DTn4MvUdbMUzckj4LjnTyJ/ThWavvg4Ar/AgMBAAECggEAExS6NIK4TZLmmWbV3sMM/94O2OG/vSO6TL8HPK8M+3UT0qrvZ+OJohyqjMz0UBayDdDF0F8J9Cmx38I6WTu/5iliShPeaNWp5KjEUevpYfd3ayVaDKlRZw3omar74I6GiDiplxK2AF2THLhwDGnpOaArV/TxdMVafZrMfb9Hiau1EBVSnuDZxlOZ4txWXOUo4XZffOWWeWBEn3nswyW5c/T1bXXR0pEV4jHgar5rfE5iyYSB0wopTK79gME259WiGoLNCclRp6LmcAC+SS4mdikMF/vK38bmC0XVbmvT8pGLktLo31iccI/kmMonXXeS4PYYsf42WfX/pVyYrTe5UQKBgQDrKbMO446HoXPq5y3rgOwA5/0tGqIcHJa0CiIgLrgOq0P/o1A2omWBi3ZNj2lx0jZXWl+eYpzzYCtc3Rb/NLRLtwNI16ISj32wupqpoZF3aOYXao0CIrOHvmlk215lhe/jZCJFGxMQMU3oRwR3n4rGk2pkcyu+FG7Lz+56nu1kbwKBgQC2tuBO3AmMSKidTu/n1zLtUrpPa50O3vibY/M5vFKxXOBgjxuCXsOJlFQLUef6XKg2Kix761L23/N1+0m6hZkRvPVqo+nJiM9i1MF2T87IEY0T3fOpkV/urZ+VTtHzM5PUy1NZ7UhNximAW6P4mxkjxVslEVIPUMMGjKDkFWuqcQKBgHS8hYONmNJhZl6klhoDLKSihP6f6vtTgcgsA5bAOmQlxqKfWpFUelkddwO3flufQ5SHa1H/l/QEuGHn6qOTcTDoz7uYN39JNdbGO0Kbzs9dBmN3ZDo8Kazn6//CGhEBtPzHV2/Aqezu3MLChkgvrb83rKfiuEMqd3m7ZaH/RM8HAoGAE99gFjOodjGQ5UsonLazO0y8Jtc1heCKeZwtYCIzfEb1wIrQGyXjbptLaM/Hw1tAkGOV4wR9SYyiFjr2kerl8x/yMV8SlnDi3YT2Zb/YZbpC9d42GkNNg9AugzbHbwQ1F/flYOqF6q+a4/MTqn50BYf5mdaAbloCXDv/Y89QnoECgYEA3BQ681944ZizeXXcmNRgJegYm3GIgAVJO+OY6bHjDdh+RyalpvGGgBYgZfHo6+I8Sw+8mKNkfOrOiTFjCV5T//WqwYLFF0uxcsV1FpHnKlZL89TsAv3iMJFfVRpxjRKx7KusG99U2Y6ysDInv/4z+lfns3ZUyEZSyfTuARVY0OY=',
                        '11000000000000035208' => 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDstuvg4bRKJsDglMNnJukciJFc+bMjSncIL91mivXn1/vPo615Wd730NxT8N7qLNvTNOEXEb66Cdl/YNMnYoVl3L234UublC1D7iLXpmufzKCUcRW6abr+fQ0B4AMsCTNrn8r+JVMnO+Xj2aprlah6xeKhqUQL3bjv/OQgnfhH9P9Gv11sw6Ud/wiq54P+xfYahMCK41JgjKPdCDau2euZK0tY3vQADbzrdJzGC3W6U6Lv/Z89v0jYS54rJr/8Z4LRdHo+ao1W/FMAyOynt9NVF66ddnzGR0zfLPZLbjnU3isICY0istCrvQt45/GwpYNey018R3cDn2Tn1NNSBfifAgMBAAECggEAHWGVBIwvSxblUNrWKCsJuytuU4qAxCwdeywpwG0WGalSzZKE2S+g/8RyXI7ktE9OF7iUCb9/m2rWjrtvag+N/Dc6ayTxNAXf2nMn6vlymVqWQeaJamJuPeE6qJVr/17C/aRPV109pXyHS9E8GG9ygo6mUyeF1S03o74ReMTyyMdwfdnJi9XAXo0It2QFG9N7AyFa82hA/K7VphYeKKP7kj+KUL1W58m/tCI28YAi1C8dy5260K5B/V2XY1MXYMe5zwSHtvgkQAdqJkStbGRNLCOqGDzbDKSSUODPMy3Nr8+xHdoutqc3HHHgHOL7DrA3QouUMzI/XgDxPBVhP6nrCQKBgQD1z7Gbe4z6fG5/9vcTUzEEzAnK6FHibSlRkcDAUvNdnxSlbYjIXioBvBs3BwsV3R3EIbzOlOj9HVVcW2oL4UPnSr+dOIKkbg7hSwYGDARezb8lTpQpqEbq98Mjum9GnyizAI1Ifm92j3IQiiFnYAmwto6sKVRpIO65+dfnrlldeQKBgQD2hrOk1/LjNM3gytOs/TLiZs+9U0lXP4GF6KgeEzLJQsRZh9Yka+mBoN4Hc9ix7hyttCm7u/QP2mIRgZx8mGvktD/9J6WwKZhTVbe09e/MccsdUZ+lASeUqwhSv2pVPO9svttfAPJ2pksOnAkeZVbjM+DWbCjF51aLFI1MaTY41wKBgHAneXPlpWdMiuZb+lOY646ywe0QnvverrGG/RlDP8MBqFvqZDR3slbkSs6KqiYpo0Ev24OuMbf7QE1JcFNHTsIjOtYzSHhZtvRYVMFBjmTrH0GYXsW6/9CJh0Gv+MGSMPGrWMsw1Q6VWIfvXlpaWVGFmgc3PEpTOlkkg7wzspaRAoGBAIby17Xd7HyJdEWgYd0pQUiD5SYLSHN1NQ67fAPymHNplBg6bnPDirBGAiRsOuuzn4lKvSmiSoi/D73riGEAAtfP/9Mxd9aF/ndnzcdGdX0hsYEcJFrr1eHg+g5p7+g3Eg0iMO3Bk/h1MVMPAULdUIHQ7TwTOwKSj2JU3sA8O81DAoGAVN/Ynur3nxzDTxX3UxqhFpiFdFkoLqRDEB8N7EtcFKg7ku/rv//RSqWATHTpf22g0sif4qADk9t09r4WmKsvN+Rsn1SxdLum6nyLl3INEbUUx32WdZ9yT0u7y7JIM6CPhvAxu1FIwgmSmguauqC7DE6IbeTI+57dVwwdB2P5EO0=',
                    ];
                    if (isset($area[$appid]) && !empty($area[$appid])) {
                        $signkey = $area[$appid];
                    }

                    if (!empty($request->get('signkey'))) {
                        $signkey = $request->get('signkey');
                    }

                    $in = [
                        'store_id' => $store_id,
                        'config_id' => $config_id,
                        'apikey' => $MerchantId,
                        'signkey' => $signkey,
                        'appid'=>$appid
                    ];
                    if ($data) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        LianfuStores::where('store_id', $store_id)->update($in);
                    } else {
                        LianfuStores::create($in);
                    }

                    //默认支付通道未注册成功
                    // $data['store_id'] = $store_id;
                    // $data['rate'] = $rate;
                    // $data['status'] = $pay_status;
                    // $data['status_desc'] = $pay_status_desc;
                    // $data['company'] = 'lianfu';
                    // $return = $this->send_ways_data($data);
                    DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'lianfu')->update(array('status'=>1,'status_desc'=>'开通成功'));
                    $return = array('status'=>1,'message'=>'修改成功');

                    return $return;
                }
            }


            //直付通
            if (15999 < $ways_type && $ways_type < 16999) {
                $data = AlipayZftStore::where('store_id', $store_id)
                    ->select('smid as MerchantId', 'order_id')
                    ->first();

                if ($MerchantId == '') {
                    if ($data) {
                        return json_encode([
                            'status' => 1,
                            'data' => $data
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    if ($data) {
                        $order_id = $data->order_id;
                        //配置
                        $isvconfig = new AlipayIsvConfigController();
                        $config_type = '03';//直付通
                        $config = $isvconfig->AlipayIsvConfig($config_id, $config_type);
                        if ($config) {
                            $aop = new AopClient();
                            $aop->useHeader = false;
                            $aop->appId = $config->app_id;
                            $aop->rsaPrivateKey = $config->rsa_private_key;
                            $aop->alipayrsaPublicKey = $config->alipay_rsa_public_key;
                            $aop->gatewayUrl = $config->alipay_gateway;
                            $aop->apiVersion = '1.0';
                            $aop->signType = 'RSA2';
                            $aop->format = 'json';
                            $aop->method = "ant.merchant.expand.order.query";
                            $request = new AntMerchantExpandOrderQueryRequest();
                            $request->setBizContent("{" .
                                "\"order_id\":\"" . $order_id . "\"" .
                                "  }");
                            $result = $aop->execute($request);

                            $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
                            $resultCode = $result->$responseNode->code;
                            if (!empty($resultCode) && $resultCode == 10000) {
                                $ext_info = $result->$responseNode->ext_info;
                                $data = json_decode($ext_info, true);
                                //成功
                                if ($result->$responseNode->status == "99") {
                                    $smid = $data['smid'];
                                    $card_alias_no = isset($data['card_alias_no']) ? $data['card_alias_no'] : "";

                                    AlipayZftStore::where('store_id', $store_id)
                                        ->update([
                                            'ext_info' => $ext_info,
                                            'status' => '99',
                                            'smid' => $smid,
                                            'card_alias_no' => $card_alias_no,
                                        ]);

                                    $data_up = [
                                        'status' => 1,
                                        'status_desc' => '审核成功',
                                    ];

                                    StorePayWay::where('store_id', $store_id)
                                        ->where('company', 'zft')
                                        ->update($data_up);

                                }

                                //失败
                                if ($result->$responseNode->status == "-1") {
                                    AlipayZftStore::where('store_id', $store_id)
                                        ->update([
                                            'ext_info' => $ext_info,
                                            'status' => '-1',
                                        ]);

                                    $data_up = [
                                        'status' => 3,
                                        'status_desc' => '审核失败',
                                    ];

                                    StorePayWay::where('store_id', $store_id)
                                        ->where('company', 'zft')
                                        ->update($data_up);
                                }

                                //审核中
                                $data_up = [
                                    'status' => 2,
                                    'status_desc' => '等待商户支付宝确认',
                                ];

                            }

                        }
                    } else {
                        $in = [
                            'store_id' => $store_id,
                            'config_id' => $config_id,
                            'smid' => $MerchantId,
                            'order_id' => '1',
                            'ip_role_id' => '1',
                            'ext_info' => '1',
                            'store_name' => $store_name,
                        ];
                        if ($data) {
                            $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                                "user_id"=>$token->user_id
                            ]);
                            if($num>0){
                                return json_encode([
                                    'status' => 2,
                                    'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                                ]);
                            }
                            AlipayZftStore::where('store_id', $store_id)->update($in);
                        } else {
                            AlipayZftStore::create($in);
                        }
                    }

                    //默认支付通道未注册成功
                    // $data['store_id'] = $store_id;
                    // $data['rate'] = $rate;
                    // $data['status'] = $pay_status;
                    // $data['status_desc'] = $pay_status_desc;
                    // $data['company'] = 'zft';
                    // $return = $this->send_ways_data($data);
                    DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'zft')->update(array('status'=>1,'status_desc'=>'开通成功'));
                    $return = array('status'=>1,'message'=>'修改成功');

                    return $return;
                }
            }

            //苏州银行
            if (17000 < $ways_type && $ways_type < 17999) {
                $data = SuzhouStore::where('store_id', $store_id)
                    ->select('MerchantId')->first();
                if ($MerchantId == '') {
                    if ($data) {
                        return json_encode([
                            'status' => 1,
                            'data' => $data
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    $in = [
                        'store_id' => $store_id,
                        'config_id' => $config_id,
                        'MerchantId' => $MerchantId,
                    ];
                    if ($data) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        SuzhouStore::where('store_id', $store_id)->update($in);
                    } else {
                        SuzhouStore::create($in);
                    }

                    //默认支付通道未注册成功
                    // $data['store_id'] = $store_id;
                    // $data['rate'] = $rate;
                    // $data['status'] = $pay_status;
                    // $data['status_desc'] = $pay_status_desc;
                    // $data['company'] = 'suzhou';
                    // $return = $this->send_ways_data($data);
                    DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'suzhou')->update(array('status'=>1,'status_desc'=>'开通成功'));
                    $return = array('status'=>1,'message'=>'修改成功');
                    return $return;
                }
            }

            //葫芦天下
            if (22999 < $ways_type && $ways_type < 23999) {
                $HltxStore = HltxStore::where('store_id', $store_id)->first();
                $obj = new ManageController();
                $hltx_config = $obj->pay_config($config_id);
                if (!$hltx_config) {
                    return json_encode(['status' => 2, 'message' => 'HL通道配置不存在']);
                }
                $obj->init($hltx_config);

                if ($MerchantId == '') {
                    if ($HltxStore) {
                        //查询状态
                        if ($HltxStore->serial_no) {
                            $re = $obj->hltx_merchant_result_query($HltxStore->serial_no);
                            // dd($re);
                        }

                        return json_encode([
                            'status' => 1,
                            'data' => [
                                'MerchantId' => $HltxStore->mer_no,
                                'wx_merchant_no' => $HltxStore->wx_merchant_no,
                                'ali_merchant_no' => $HltxStore->ali_merchant_no,
                                'union_merchant_no' => $HltxStore->union_merchant_no,
                                'qd' => $HltxStore->qd
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    $in = [
                        'store_id' => $store_id,
                        'config_id' => $config_id,
                        'mer_no' => $MerchantId,
                        'status' => '00',
                        'qd' => $qd
                    ];
                    if ($HltxStore) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        HltxStore::where('store_id', $store_id)->update($in);
                    } else {
                        HltxStore::create($in);
                    }
                    $params = [
                        'merNo' => $MerchantId,
                        'path' => $weixin_path ? $weixin_path : env('APP_URL') . '/api/hltx/weixin/',
                        'appId' => $hltx_config->pay_appid,
                        'subScribeAppIds' => $hltx_config->subscribe_appid,
                    ];

                    $obj->merchant_wxsetconig($params);

                    //默认支付通道未注册成功
                    // $data['store_id'] = $store_id;
                    // $data['rate'] = $rate;
                    // $data['status'] = $pay_status;
                    // $data['status_desc'] = $pay_status_desc;
                    // $data['company'] = 'hltx';
                    // $return = $this->send_ways_data($data);
                    DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'hltx')->update(array('status'=>1,'status_desc'=>'开通成功'));
                    $return = array('status'=>1,'message'=>'修改成功');

                    return $return;
                }
            }

            //长沙银行
            if (25000 < $ways_type && $ways_type < 25999) {
                $data = ChangshaStore::where('store_id', $store_id)
                    ->select('ECustId as MerchantId', 'DeptId', 'StaffId')->first();
                if ($MerchantId == '') {
                    if ($data) {
                        return json_encode([
                            'status' => 1,
                            'data' => $data
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    $in = [
                        'store_id' => $store_id,
                        'config_id' => $config_id,
                        'ECustId' => $MerchantId,
                        'DeptId' => $DeptId,
                        'StaffId' => $StaffId
                    ];

                    if ($data) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        ChangshaStore::where('store_id', $store_id)->update($in);
                    } else {
                        ChangshaStore::create($in);
                    }

                    //默认支付通道未注册成功
                    // $data['store_id'] = $store_id;
                    // $data['rate'] = $rate;
                    // $data['status'] = $pay_status;
                    // $data['status_desc'] = $pay_status_desc;
                    // $data['company'] = 'changsha';
                    // $return = $this->send_ways_data($data);
                    DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'changsha')->update(array('status'=>1,'status_desc'=>'开通成功'));
                    $return = array('status'=>1,'message'=>'修改成功');
                    return $return;
                }
            }

            //邮储
            if (26000 < $ways_type && $ways_type < 26999) {
                $you_type = $request->get('you_type', '');
                if($you_type=='3'){
                    $platformID='300370800006480';
                        $certNum='1451489200';
                        $reqSysId='GIZ00001307';
                        $prikey='6a5dc556f72f8f186216e2cf2cfadbc70a4f900083234e10f5c9c5affc36842a';
                        $pubkey='04b60bf1e7a12bff87a4ecc130b472f15c915e6ff18010a3c6f1c19979649ef03be8e4c2561eef8a473491ee8131d908967219a6d34999284cb82d6bfa3a883f18';
                }else if($you_type=='4'){
                    $platformID='300340100007387';
                    $certNum='1488619724';
                    $reqSysId='GIZ00001507';
                    $prikey='0221b5cf7eb87fdc2c4a45c239989a9a5765cbeb04935a90fa51dc2dd23d0b37';
                    $pubkey='0491ff67c7d2f56fb4eb943d84178b297448fad4ed9756e9318362977a87692318183aad374f15830d88a1f9265f746db68e4b41a7f6089105d510bf83d9af4e04';
                }else if($you_type=='5'){
                    $platformID='300610100008704';
                    $certNum='1541933287';
                    $reqSysId='GIZ00001643';
                    $prikey='64c63a510a6e9c943cd5caa8eb05cc3edf5694a6ee5eb38bcde15591196f0d9c';
                    $pubkey='04074c885e27268777bdb1d326a88fa022a0b0e1994674a4070953a3dc93391348899c5769b6bb224e659388a5aca2ef00f6202999d01740ea34627ca320d3c695';
                } else{
                    if($token->user_id=='236'){
                        $platformID='300610100004854';
                        $certNum='1424820092';
                        $reqSysId='GIZ00000958';
                        $prikey='82ff7a19133bda1d4a2c9460c57763625bcd95de102f3edb5556be278050cfb8';
                        $pubkey='78232c3993b5f1e4ef55559c6db86ad1180d8f8308f22d51ca77ec398fa83110f5dba5c69a22336cf7704aeacc1283e823157342ded1c5d7e24c8bd57d248e92';
                    }else{
                        $platformID='300340100002864';
                        $certNum='1390171054';
                        $reqSysId='GIZ00000520';
                        $prikey='e3155bb90c1b4cbef128bd21cf6326dd45747f1445c0d05c4d2f30492567f348';
                        $pubkey='337A144065FED2DFC8A6CC191F72A66B68BB72EB981775C0275ECA9FC9421FF024F691DDC6D2078C072FE33404DD8B875A5F5ECE1A70A522869549F0478D9F75';
                    }
                }

                $store=Store::where('store_id', $store_id)->first();
                if($MerchantId){
                    $deviced_id='Wft'.mt_rand(10000,99999);
                    $post_data = [
                        'version' => '1.0',
                        'txnCode' => '8019',
                        'channelID' => '08',
                        'platformID' => $platformID,
                        'reqTraceID' => time(),
                        'reqDate' => date('YmdHis',time()),
                        'certNum' => $certNum,
                        'reqSysId' => $reqSysId,
                        'isSign' => '1',
                        'externalType'=>'02',
                        'mchtId'=>$MerchantId,
                        'operationId'=>'00',
                        'devicedId'=>$deviced_id,
                        'deviceState'=>'00',
                        'deviceType'=>'13',
                        'deviceAddress'=>$store->province_name.'-'.$store->city_name.'-'.$store->area_name.'-'.$store->store_address,
                    ];
                    $post_data = array_filter($post_data);
                    ksort($post_data);//整理数组排序
                    $string=$this->get_sign($post_data);
                    $sm2 = new RtSm2('base64');
                    $sign = $sm2->doSign(
                        $string,
                        $prikey,
                        $pubkey,
                        '1234567812345678');
                    $sign=trim($sign);
                    $post_data['signature']=$sign;
                    $url = 'https://nap.psbc.com/trans/intermgr/online/api/platform/qrserviceNet/termService/externalPos';
                    $re = $this->http_curl1($url, json_encode($post_data));
                    $re=json_decode($re,true);
                    if ($re['respCd']!='0000') {
                        return json_encode(['status' => 2, 'message' => $re['respDesc']]);
                    }
                    if($re['respCd']=='0000'){
                        $pos_sn=$deviced_id;
                        $signkey=1;
                    }else{
                        $pos_sn='';
                        $signkey=0;
                    }
                }
                $data = LianfuyoupayStores::where('store_id', $store_id)
                    ->select(
                        'apikey as MerchantId',
                        'signkey',
                        'pos_sn',
                        "type"
                    )
                    ->first();

                if ($MerchantId == '') {
                    if ($data) {
                        return json_encode([
                            'status' => 1,
                            'data' => $data
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    $in = [
                        'store_id' => $store_id,
                        'config_id' => $config_id,
                        'apikey' => $MerchantId,
                        'signkey' => $signkey,
                        'pos_sn' => $pos_sn,
                        'type' => $you_type,
                    ];
                    if ($data) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        LianfuyoupayStores::where('store_id', $store_id)->update($in);
                    } else {
                        LianfuyoupayStores::create($in);
                    }


                    //默认支付通道未注册成功
                    // $data['store_id'] = $store_id;
                    // $data['rate'] = $rate;
                    // $data['status'] = $pay_status;
                    // $data['status_desc'] = $pay_status_desc;
                    // $data['company'] = 'lianfuyoupay';
                    // $return = $this->send_ways_data($data);
                    DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'lianfuyoupay')->update(array('status'=>1,'status_desc'=>'开通成功'));
                    $return = array('status'=>1,'message'=>'修改成功');
                    return $return;
                }
            }

            //海科融通
            if (21999 < $ways_type && $ways_type < 22999) {
                $hkrt_store = HkrtStore::where('store_id', $store_id)->first();
                if (!$hkrt_store) {
                    //看下是否有修改权限
                    $hasPermission = $user->hasPermissionTo('海科融通商户号添加');
                    if (!$hasPermission) {
                        return json_encode(['status' => 2, 'message' => '没有-海科融通商户号添加-权限']);
                    }
                } else {
                    //看下是否有添加权限
                    $hasPermission = $user->hasPermissionTo('海科融通商户号修改');
                    if (!$hasPermission) {
                        return json_encode(['status' => 2, 'message' => '没有-海科融通商户号修改-权限']);
                    }
                }

                if ($hkrtMerchNo == '') {
                    if ($hkrt_store) {
                        return json_encode([
                            'status' => '1',
                            'data' => [
                                'hkrtMerchNo' => $hkrt_store->merch_no,
                                'hkrtAgentApplyNo' => $hkrt_store->agent_apply_no
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => '1',
                            'data' => []
                        ]);
                    }
                } else {
                    $config = new HkrtConfigController();
                    $hkrt_config = $config->hkrt_config($config_id);
                    if (!$hkrt_config) {
                        return json_encode([
                            'status' => 2,
                            'message' => '海科融通配置不存在请检查配置'
                        ]);
                    }

                    //商户入驻状态查询
                    if ($hkrt_store && $hkrt_store->merch_no) {
                        $obj = new \App\Api\Controllers\Hkrt\PayController();
                        $hkrt_data = [
                            'access_id' => $hkrt_config->access_id,
                            'agent_no' => $hkrt_config->agent_no,
                            'agent_apply_no' => $hkrt_store->agent_apply_no,
                            'apply_type' => '1', //1-商户入网申请;2-微信业务申请;3-支付宝业务申请;4-银行卡业务申请;10-商户分账业务申请;11-商户分账关系申请;12-商户结算周期变更申请;13-商户分账业务修改
                            'access_key' => $hkrt_config->access_key
                        ];
//                        Log::info('海科融通-商户入驻状态查询');
//                        Log::info($hkrt_data);
                        $open_query_store = $obj->merchant_query($hkrt_data); //0-系统错误 1-提交失败 2-已受理 3-自动审核 4-待审核 5-审核失败 6-审核拒绝 7-审核成功 8-审核中 9-待报备 10-报备中 11-报备失败 12-报备成功
                        Log::info('海科融通-商户入驻状态查询结果');
                        Log::info($open_query_store);
                        //审核状态
                        if ($open_query_store['status'] != '0') {
                            if ($open_query_store['status'] != '7' || $open_query_store['status'] != '3') {
                                $data_up = [
                                    'status' => 3,
                                    'status_desc' => $open_query_store['message'],
                                ];
                                StorePayWay::where('store_id', $store_id)
                                    ->where('company', 'hkrt')
                                    ->update($data_up);

                                $hkrt_update_data = [
                                    'status' => $open_query_store['status']
                                ];
                                if ($hkrtAgentApplyNo) $hkrt_update_data['agent_apply_no'] = $hkrtAgentApplyNo; //服务商申请单号
                                $hkrt_res = HkrtStore::where('store_id', $store_id)
                                    ->update($hkrt_update_data);
                                if (!$hkrt_res) {
                                    Log::info('海科融通-商户入网-入库更新失败');
                                    Log::info($store_id);
                                }

                                return json_encode([
                                    'status' => 2,
                                    'message' => $open_query_store['message'],
                                ]);
                            }
                        }
                    }

                    //读取商户的费率
                    $StorePayWay = StorePayWay::where('ways_type', $ways_type)
                        ->where('store_id', $store_id)
                        ->select('rate')
                        ->first();
                    if ($StorePayWay) {
                        $rate = $StorePayWay->rate;
                    } else {
                        return json_encode([
                            'status' => 2,
                            'message' => '商户通道费率未设置',
                        ]);
                    }

                    $hkrt_obj = new \App\Api\Controllers\Hkrt\PayController();
                    //报备微信授权目录
                    $hkrt_data = [
                        'access_id' => $hkrt_config->access_id,
                        'agent_no' => $hkrt_config->agent_no,
                        'merch_no' => $MerchantId,
                        'conf_key' => 'auth_path', //授权目录
                        'conf_value' => $weixin_path,
                        'access_key' => $hkrt_config->access_key
                    ];
                    $hkrt_res = $hkrt_obj->wx_appid_conf_add($hkrt_data); //0-失败 1-成功
                    Log::info('商户号报备微信');
                    Log::info($hkrt_res);
                    if ($hkrt_res['status'] == '1') {
                        Log::info('商户号报备微信成功');
                    }

                    if ($hkrt_store) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        HkrtStore::where('store_id', $store_id)
                            ->update([
                                'merch_no' => $MerchantId
                            ]);
                    } else {
                        HkrtStore::create([
                            'store_id' => $store_id,
                            'config_id' => $config_id,
                            'merch_no' => $MerchantId
                        ]);
                    }
                }

                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)
                    ->where('ways_type', 22001) //目前是一样的直接读取支付宝就行
                    ->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => 2,
                        'message' => '请联系代理商开启此通道',
                    ]);
                }

                //默认支付通道未注册成功
                // $data['store_id'] = $store_id;
                // $data['rate'] = $rate;
                // $data['status'] = $pay_status;
                // $data['status_desc'] = $pay_status_desc;
                // $data['company'] = 'hkrt';
                // $return = $this->send_ways_data($data);
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'hkrt')->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');

                return $return;
            }

            //易生支付
            if(in_array($ways_type,[21001,21002,21004,61001,61002,61004])){
                $version="easypay";
                if(in_array($ways_type,[61001,61002,61004])){
                    $version="easypaya";
                }
                $query=EasypayStore::query();
                if($version=="easypaya"){
                    $query=EasypayaStore::query();
                }
                $easypaystore = $query->where('store_id', $store_id)->first();
                $easyPrivateKey = $request->get('easyPrivateKey', 0);
                if (!$easypaystore) {
                    $hasPermission = $user->hasPermissionTo('易生支付商户号添加');
                    if (!$hasPermission) {
                        return json_encode([
                            'status' => 2,
                            'message' => '没有-易生支付商户号添加-权限'
                        ]);
                    }
                } else {
                    $hasPermission = $user->hasPermissionTo('易生支付商户号修改');
                    if (!$hasPermission) {
                        return json_encode([
                            'status' => 2,
                            'message' => '没有-易生支付商户号修改-权限'
                        ]);
                    }
                }

                if ($easyPayMerCode == '' && $easyPayTermCode == '') {
                    if ($easypaystore) {
                        return json_encode([
                            'status' => '1',
                            'data' => [
                                'operaTrace' => $easypaystore->operaTrace,
                                'merTrace' => $easypaystore->mer_trace,
                                'easyPayMerCode' => $easypaystore->termMercode,
                                'easyPayTermCode' => $easypaystore->termTermcode,
                                 'easyPrivateKey' => $easypaystore->private_key
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => '1',
                            'data' => []
                        ]);
                    }
                } else {
                    $config = new EasyPayConfigController();
                    $easypayconfig = $config->easypay_config($config_id);
                    if (!$easypayconfig) {
                        return json_encode([
                            'status' => '2',
                            'message' => '易生支付配置不存在请检查配置'
                        ]);
                    }

                    //商户审核结果查询
//                    if ($easypaystore && $easypaystore->operaTrace) {
//                        $obj = new \App\Api\Controllers\Easypay\PayController();
//                        $easypaydata = [
//                            'clientCode' => $easypayconfig->client_code, //机构号
//                            'merTrace' => $easypaystore->mer_trace ?? '', //O,商户唯一标识
//                            'operaTrace' => $easypaystore->operaTrace, //原操作流水号
//                        ];
//                        Log::info('易生支付-商户审核结果查询');
//                        Log::info($easypaydata);
//                        $open_query_store = $obj->merchant_query($easypaydata); //-1 系统错误 9-待处理；3-待审核；0-审核通过；X-审核失败；1-审核驳回
//                        Log::info($open_query_store);
//
//                        //审核状态
//                        if ($open_query_store['status'] != '-1') {
//                            if ($open_query_store['status'] == '0') {
//                                $data_up = [
//                                    'status' => 1,
//                                    'status_desc' => $open_query_store['message'],
//                                ];
//                                StorePayWay::where('store_id', $store_id)
//                                    ->where('company', 'easypay')
//                                    ->update($data_up);
//
//                                EasypayStore::where('store_id', $store_id)
//                                    ->update([
//                                        'audit_status' => 0,
//                                        'audit_msg' => $open_query_store['message']
//                                    ]);
//                            } else {
//                                $data_up = [
//                                    'status' => 3,
//                                    'status_desc' => $open_query_store['message'],
//                                ];
//                                StorePayWay::where('store_id', $store_id)
//                                    ->where('company', 'easypay')
//                                    ->update($data_up);
//
//                                EasypayStore::where('store_id', $store_id)
//                                    ->update([
//                                        'audit_status' => 0,
//                                        'audit_msg' => $open_query_store['message']
//                                    ]);
//
//                                return json_encode([
//                                    'status' => '2',
//                                    'message' => $open_query_store['message'],
//                                ]);
//                            }
//                        }
//                    }

                    //易生平台进件成功
                    StorePayWay::query()->where('store_id', $store_id)
                        ->where('company', $version)
                        ->update([
                            'status' => '1',
                            'status_desc' => '开通成功'
                        ]);

                    //读取商户的费率
                    $StorePayWay = StorePayWay::query()->where('ways_type', $ways_type)
                        ->where('store_id', $store_id)
                        ->select('rate')
                        ->first();
                    if ($StorePayWay) {
                        $rate = $StorePayWay->rate;
                    } else {
                        return json_encode([
                            'status' => 2,
                            'message' => '商户通道费率未设置',
                        ]);
                    }

//                    $easypayobj = new \App\Api\Controllers\easypay\PayController();
//                    //报备微信授权目录
//                    $easypaydata = [
//                        'access_id' => $easypayconfig->access_id,
//                        'agent_no' => $easypayconfig->agent_no,
//                        'merch_no' => $MerchantId,
//                        'conf_key' => 'auth_path', //授权目录
//                        'conf_value' => $weixin_path,
//                        'access_key' => $easypayconfig->access_key
//                    ];
//                    $easypayres = $easypayobj->wx_appid_conf_add($easypaydata); //0-失败 1-成功
//                    Log::info('商户号报备微信');
//                    Log::info($easypayres);
//                    if ($easypayres['status'] == '1') {
//                        Log::info('商户号报备微信成功');
//                    }

                    if ($easypaystore) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        $query->where('store_id', $store_id)
                            ->update([
//                                'private_key' => $easyPrivateKey,
//                                'operaTrace' => $easyPayOperaTrace,
                                'termMercode' => $easyPayMerCode,
                                'termTermcode' => $easyPayTermCode
                            ]);
                    } else {
                        $query->create([
                            'store_id' => $store_id,
                            'config_id' => $config_id,
//                            'private_key' => $easyPrivateKey,
//                            'operaTrace' => $easyPayOperaTrace,
                            'termMercode' => $easyPayMerCode,
                            'termTermcode' => $easyPayTermCode,
                            'audit_status'=> '0',
                            'audit_msg'=> '审核通过'
                        ]);
                    }

                }

                if($version=="easypay"){
                    //费率 默认商户的费率为代理商的费率
                    $UserRate = UserRate::where('user_id', $Store->user_id)
                        ->where('ways_type', 21001) //目前是一样的直接读取支付宝就行
                        ->first();
                }else{
                    $UserRate = UserRate::where('user_id', $Store->user_id)
                        ->where('ways_type', 61001) //目前是一样的直接读取支付宝就行
                        ->first();
                }
                if (!$UserRate) {
                    return json_encode([
                        'status' => 2,
                        'message' => '请联系代理商开启此通道',
                    ]);
                }

                //默认支付通道未注册成功
                // $data['store_id'] = $store_id;
                // $data['rate'] = $rate;
                // $data['status'] = $pay_status;
                // $data['status_desc'] = $pay_status_desc;
                // $data['company'] = 'easypay';
                // $return = $this->send_ways_data($data);
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', $version)->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');

                return $return;
            }

            //易生支付a
            if (false && 60999 < $ways_type && $ways_type < 61999) {
                $easypaystore = \App\Models\EasypayaStore::where('store_id', $store_id)->first();
                $easyPrivateKey = $request->get('easyPrivateKey', 0);
                if (!$easypaystore) {
                    $hasPermission = $user->hasPermissionTo('易生支付a商户号添加');
                    if (!$hasPermission) {
                        return json_encode([
                            'status' => 2,
                            'message' => '没有-易生支付a商户号添加-权限'
                        ]);
                    }
                } else {
                    $hasPermission = $user->hasPermissionTo('易生支付a商户号修改');
                    if (!$hasPermission) {
                        return json_encode([
                            'status' => 2,
                            'message' => '没有-易生支付a商户号修改-权限'
                        ]);
                    }
                }

                if ($easyPayMerCode == '' && $easyPayTermCode == '') {
                    if ($easypaystore) {
                        return json_encode([
                            'status' => '1',
                            'data' => [
                                'operaTrace' => $easypaystore->operaTrace,
                                'merTrace' => $easypaystore->mer_trace,
                                'easyPayMerCode' => $easypaystore->termMercode,
                                'easyPayTermCode' => $easypaystore->termTermcode,
//                                'easyPrivateKey' => $easypaystore->private_key
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => '1',
                            'data' => []
                        ]);
                    }
                } else {
                    $config = new EasyPayConfigController();
                    $easypayconfig = $config->easypay_config($config_id);
                    if (!$easypayconfig) {
                        return json_encode([
                            'status' => '2',
                            'message' => '易生支付a配置不存在请检查配置'
                        ]);
                    }

                    //商户审核结果查询
//                    if ($easypaystore && $easypaystore->operaTrace) {
//                        $obj = new \App\Api\Controllers\Easypay\PayController();
//                        $easypaydata = [
//                            'clientCode' => $easypayconfig->client_code, //机构号
//                            'merTrace' => $easypaystore->mer_trace ?? '', //O,商户唯一标识
//                            'operaTrace' => $easypaystore->operaTrace, //原操作流水号
//                        ];
//                        Log::info('易生支付-商户审核结果查询');
//                        Log::info($easypaydata);
//                        $open_query_store = $obj->merchant_query($easypaydata); //-1 系统错误 9-待处理；3-待审核；0-审核通过；X-审核失败；1-审核驳回
//                        Log::info($open_query_store);
//
//                        //审核状态
//                        if ($open_query_store['status'] != '-1') {
//                            if ($open_query_store['status'] == '0') {
//                                $data_up = [
//                                    'status' => 1,
//                                    'status_desc' => $open_query_store['message'],
//                                ];
//                                StorePayWay::where('store_id', $store_id)
//                                    ->where('company', 'easypay')
//                                    ->update($data_up);
//
//                                EasypayStore::where('store_id', $store_id)
//                                    ->update([
//                                        'audit_status' => 0,
//                                        'audit_msg' => $open_query_store['message']
//                                    ]);
//                            } else {
//                                $data_up = [
//                                    'status' => 3,
//                                    'status_desc' => $open_query_store['message'],
//                                ];
//                                StorePayWay::where('store_id', $store_id)
//                                    ->where('company', 'easypay')
//                                    ->update($data_up);
//
//                                EasypayStore::where('store_id', $store_id)
//                                    ->update([
//                                        'audit_status' => 0,
//                                        'audit_msg' => $open_query_store['message']
//                                    ]);
//
//                                return json_encode([
//                                    'status' => '2',
//                                    'message' => $open_query_store['message'],
//                                ]);
//                            }
//                        }
//                    }

                    //易生平台进件成功
                    StorePayWay::where('store_id', $store_id)
                        ->where('company', 'easypaya')
                        ->update([
                            'status' => '1',
                            'status_desc' => '开通成功'
                        ]);

                    //读取商户的费率
                    $StorePayWay = StorePayWay::where('ways_type', $ways_type)
                        ->where('store_id', $store_id)
                        ->select('rate')
                        ->first();
                    if ($StorePayWay) {
                        $rate = $StorePayWay->rate;
                    } else {
                        return json_encode([
                            'status' => 2,
                            'message' => '商户通道费率未设置',
                        ]);
                    }

//                    $easypayobj = new \App\Api\Controllers\easypay\PayController();
//                    //报备微信授权目录
//                    $easypaydata = [
//                        'access_id' => $easypayconfig->access_id,
//                        'agent_no' => $easypayconfig->agent_no,
//                        'merch_no' => $MerchantId,
//                        'conf_key' => 'auth_path', //授权目录
//                        'conf_value' => $weixin_path,
//                        'access_key' => $easypayconfig->access_key
//                    ];
//                    $easypayres = $easypayobj->wx_appid_conf_add($easypaydata); //0-失败 1-成功
//                    Log::info('商户号报备微信');
//                    Log::info($easypayres);
//                    if ($easypayres['status'] == '1') {
//                        Log::info('商户号报备微信成功');
//                    }

                    if ($easypaystore) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        \App\Models\EasypayaStore::where('store_id', $store_id)
                            ->update([
//                                'private_key' => $easyPrivateKey,
//                                'operaTrace' => $easyPayOperaTrace,
                                'termMercode' => $easyPayMerCode,
                                'termTermcode' => $easyPayTermCode
                            ]);
                    } else {
                        \App\Models\EasypayaStore::create([
                            'store_id' => $store_id,
                            'config_id' => $config_id,
//                            'private_key' => $easyPrivateKey,
//                            'operaTrace' => $easyPayOperaTrace,
                            'termMercode' => $easyPayMerCode,
                            'termTermcode' => $easyPayTermCode,
                            'audit_status'=> '0',
                            'audit_msg'=> '审核通过'
                        ]);
                    }

                }

                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)
                    ->where('ways_type', 61001) //目前是一样的直接读取支付宝就行
                    ->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => 2,
                        'message' => '请联系代理商开启此通道',
                    ]);
                }
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'easypaya')->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');

                return $return;
            }

            //联动优势
            if (4999 < $ways_type && $ways_type < 5999) {
                $linkageStore = LinkageStores::where('store_id', $store_id)->first();
                if (!$linkageStore) {
                    $hasPermission = $user->hasPermissionTo('联动优势商户号添加');
                    if (!$hasPermission) {
                        return json_encode([
                            'status' => '2',
                            'message' => '没有-联动优势商户号添加-权限'
                        ]);
                    }
                } else {
                    $hasPermission = $user->hasPermissionTo('联动优势商户号修改');
                    if (!$hasPermission) {
                        return json_encode([
                            'status' => '2',
                            'message' => '没有-联动优势商户号修改-权限'
                        ]);
                    }
                }

                if ($linkageAcqMerId == '') {
                    if ($linkageStore) {
                        return json_encode([
                            'status' => '1',
                            'data' => [
                                'acqMerId' => $linkageStore->acqMerId, //商户号(联动平台分配)
                                'subAppid' => $linkageStore->subAppid, //子商户SubAPPID
                                'subMchId' => $linkageStore->subMchId, //微信子商户号
                                'merId' => $linkageStore->merId //报备编号
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => '1',
                            'data' => []
                        ]);
                    }
                } else {
                    $config = new LinkageConfigController();
                    $linkageConfig = $config->linkage_config($config_id);
                    if (!$linkageConfig) {
                        return json_encode([
                            'status' => '2',
                            'message' => '联动优势支付配置不存在，请检查配置'
                        ]);
                    }

                    //读取商户的费率
                    $StorePayWay = StorePayWay::where('ways_type', $ways_type)
                        ->where('store_id', $store_id)
                        ->select('rate')
                        ->first();
                    if ($StorePayWay) {
                        $rate = $StorePayWay->rate;
                    } else {
                        return json_encode([
                            'status' => '2',
                            'message' => '商户通道费率未设置'
                        ]);
                    }

                    $linkageObj = new \App\Api\Controllers\Linkage\PayController();
                    if ($weixin_path) {
                        //微信参数配置-支付授权目录
                        $linkageData = [
                            'acqSpId' => $linkageConfig->mch_id, //服务商编号(联动平台分配)
                            'acqMerId' => $linkageAcqMerId, //商户号
                            'jsapiPath' => $weixin_path ? $weixin_path : env('APP_URL') . '/api/linkage/weixin/',
                            'publicKey' => $linkageConfig->publicKey,
                            'privateKey' => $linkageConfig->privateKey
                        ];
                        $linkageRes = $linkageObj->merchantsJsApiPath($linkageData); //-1系统错误 0-其他 1-成功 2-验签失败 3-失败
                        Log::info('联动优势-报备微信授权目录');
                        Log::info($linkageRes);
                        if ($linkageRes['status'] != '1') {
                            Log::info('联动优势-报备微信授权目录-error');
                            Log::info($linkageRes['message']);
                            return json_encode([
                                'status' => '2',
                                'message' => $linkageRes['message']
                            ]);
                        }
                    }
                    Log::info($linkageSubAppid);
                    //微信参数配置-子商户appId
                    if ($linkageSubAppid) {
                        $linkageSubData = [
                            'acqSpId' => $linkageConfig->mch_id, //服务商编号(联动平台分配)
                            'acqMerId' => $linkageAcqMerId, //商户号
                            'subAppid' => $linkageSubAppid, //子商户SubAPPID
                            'publicKey' => $linkageConfig->publicKey,
                            'privateKey' => $linkageConfig->privateKey
                        ];
                        $linkageSubRes = $linkageObj->merchantsSubAppid($linkageSubData); //-1系统错误 0-其他 1-成功 2-验签失败 3-失败
                        if ($linkageSubRes['status'] != '1') {
                            Log::info('联动优势-微信参数配置-子商户appId-error');
                            Log::info($linkageSubRes['message']);
                            return json_encode([
                                'status' => '2',
                                'message' => $linkageSubRes['message']
                            ]);
                        }
                    }

                    if ($linkageStore) {
                        $updateData = [
                            'acqMerId' => $linkageAcqMerId
                        ];
                        if ($linkageSubAppid) $updateData['subAppid'] = $linkageSubAppid;
                        if ($linkageSubMchId) $updateData['subMchId'] = $linkageSubMchId;
                        if ($linkageMerId) $updateData['merId'] = $linkageMerId;
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        $linkageStore->update($updateData);
                    } else {
                        $linkageDataInsert = [
                            'store_id' => $store_id,
                            'config_id' => $config_id,
                            'acqMerId' => $linkageAcqMerId,
                            'status'=> '1'
                        ];
                        if ($linkageMerId) $linkageDataInsert['merId'] = $linkageMerId;
                        if ($linkageSubAppid) $linkageDataInsert['subAppid'] = $linkageSubAppid;
                        if ($linkageSubMchId) $linkageDataInsert['subMchId'] = $linkageSubMchId;
                        LinkageStores::create($linkageDataInsert);
                    }

                }

                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)
                    ->where('ways_type', 5001) //目前是一样的直接读取支付宝就行
                    ->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => '2',
                        'message' => '请联系代理商开启此通道'
                    ]);
                }

                //默认支付通道未注册成功
                // $data['store_id'] = $store_id;
                // $data['rate'] = $rate;
                // $data['status'] = $pay_status;
                // $data['status_desc'] = $pay_status_desc;
                // $data['company'] = 'linkage';
                // $return = $this->send_ways_data($data);
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'linkage')->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');

                return $return;
            }

            //威富通
            if (26999 < $ways_type && $ways_type < 27999) {

                $version="v1";
                $company="wftpay";
                $tmpWaysType=27001;
                $query=WftpayStore::query();
                if(in_array($ways_type,[27101, 27102, 27103, 27104, 27105, 27107])){
                    $version="v2";
                    $company="wftpayv2";
                    $tmpWaysType=27101;
                    $query=WftpayStoreV2::query();
                }
                $wftpayStore = (clone $query)->where('store_id', $store_id)->first();
                if (!$wftpayStore) {
                    $hasPermission = $user->hasPermissionTo('威富通商户号添加');
                    if (!$hasPermission) {
                        return json_encode([
                            'status' => '2',
                            'message' => '没有-威富通商户号添加-权限'
                        ]);
                    }
                } else {
                    $hasPermission = $user->hasPermissionTo('威富通商户号修改');
                    if (!$hasPermission) {
                        return json_encode([
                            'status' => '2',
                            'message' => '没有-威富通商户号修改-权限'
                        ]);
                    }
                }

                if ($wftpayMchId == '') {
                    if ($wftpayStore) {
                        return json_encode([
                            'status' => '1',
                            'data' => [
                                'wftpayMchId' => $wftpayStore->mch_id, //商户号
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => '1',
                            'data' => []
                        ]);
                    }
                } else {
                    $config = new WftPayConfigController();
                    $wftpayConfig = $config->wftpay_config($config_id,$version);
                    if (!$wftpayConfig) {
                        return json_encode([
                            'status' => '2',
                            'message' => '威富通支付配置不存在，请检查配置'
                        ]);
                    }

                    //读取商户的费率
                    $StorePayWay = StorePayWay::where('ways_type', $ways_type)
                        ->where('store_id', $store_id)
                        ->select('rate')
                        ->first();
                    if ($StorePayWay) {
                        $rate = $StorePayWay->rate;
                    } else {
                        return json_encode([
                            'status' => '2',
                            'message' => '商户通道费率未设置'
                        ]);
                    }

                    if ($wftpayStore) {
                        $updateData = [
                            'mch_id' => $wftpayMchId
                        ];
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        $wftpayStore->update($updateData);
                    } else {
                        $wftpayDataInsert = [
                            'store_id' => $store_id,
                            'config_id' => $config_id,
                            'mch_id' => $wftpayMchId,
                            'status'=> 1
                        ];
                        (clone $query)->create($wftpayDataInsert);
                    }

                    //todo:默认第三方商户入网成功
                    $pay_status = 1;
                    $pay_status_desc = '开通成功';
                }

                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)
                    ->where('ways_type', $tmpWaysType) //目前是一样的直接读取支付宝就行
                    ->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => '2',
                        'message' => '请联系代理商开启此通道'
                    ]);
                }

                //默认支付通道未注册成功
                // $data['store_id'] = $store_id;
                // $data['rate'] = $rate;
                // $data['status'] = $pay_status;
                // $data['status_desc'] = $pay_status_desc;
                // $data['company'] = 'wftpay';
                // $return = $this->send_ways_data($data);
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', $company)->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');

                return $return;
            }

            //兴业银行
            if (38999 < $ways_type && $ways_type < 39999) {
                $cibpayStore = \App\Models\CibpayStore::where('store_id', $store_id)->first();
                if (!$cibpayStore) {
                    $hasPermission = $user->hasPermissionTo('兴业银行商户号添加');
                    if (!$hasPermission) {
                        return json_encode([
                            'status' => '2',
                            'message' => '没有-兴业银行商户号添加-权限'
                        ]);
                    }
                } else {
                    $hasPermission = $user->hasPermissionTo('兴业银行商户号修改');
                    if (!$hasPermission) {
                        return json_encode([
                            'status' => '2',
                            'message' => '没有-兴业银行商户号修改-权限'
                        ]);
                    }
                }
                $cibpayMchId = $request->get('cibpayMchId', '');
                if ($cibpayMchId == '') {
                    if ($cibpayStore) {
                        return json_encode([
                            'status' => '1',
                            'data' => [
                                'cibMchId' => $cibpayStore->mch_id, //商户号
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => '1',
                            'data' => []
                        ]);
                    }
                } else {
                    $config = new WftPayConfigController();
                    $wftpayConfig = $config->wftpay_config($config_id);
                    if (!$wftpayConfig) {
                        return json_encode([
                            'status' => '2',
                            'message' => '支付配置不存在，请检查配置'
                        ]);
                    }

                    if ($cibpayStore) {
                        $updateData = [
                            'mch_id' => $cibpayMchId
                        ];
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        $cibpayStore->update($updateData);
                    } else {
                        $cibpayDataInsert = [
                            'store_id' => $store_id,
                            'config_id' => $config_id,
                            'mch_id' => $cibpayMchId,
                            'status'=> 1
                        ];
                        \App\Models\CibpayStore::create($cibpayDataInsert);
                    }
                }

                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)
                    ->where('ways_type', 39002) //目前是一样的直接读取支付宝就行
                    ->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => '2',
                        'message' => '请联系代理商开启此通道'
                    ]);
                }
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'cib')->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');

                return $return;
            }

            //重庆银行
            if (43999 < $ways_type && $ways_type < 44999) {
                $payStore = \App\Models\CqcbankStores::where('store_id', $store_id)->first();
                $MerchantId = $request->get('MerchantId', '');
                if ($MerchantId == '') {
                    if ($payStore) {
                        return json_encode([
                            'status' => '1',
                            'data' => [
                                'mch_id' => $payStore->mch_id, //商户号
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => '1',
                            'data' => []
                        ]);
                    }
                } else {
                    $config = new WftPayConfigController();
                    $wftpayConfig = $config->wftpay_config($config_id);
                    if (!$wftpayConfig) {
                        return json_encode([
                            'status' => '2',
                            'message' => '支付配置不存在，请检查配置'
                        ]);
                    }

                    if ($payStore) {
                        $updateData = [
                            'mch_id' => $MerchantId
                        ];
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        $payStore->update($updateData);
                    } else {
                        $insert = [
                            'store_id' => $store_id,
                            'config_id' => $config_id,
                            'mch_id' => $MerchantId,
                            'status'=> 1
                        ];
                        \App\Models\CqcbankStores::create($insert);
                    }
                }

                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)
                    ->where('ways_type', 44002) //目前是一样的直接读取支付宝就行
                    ->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => '2',
                        'message' => '请联系代理商开启此通道'
                    ]);
                }
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'cqcbank')->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');

                return $return;
            }

            //汇旺财
            if (27999 < $ways_type && $ways_type < 28999) {
                $hwcpayStore = HwcpayStore::where('store_id', $store_id)->first();
                if (!$hwcpayStore) {
                    $hasPermission = $user->hasPermissionTo('汇旺财商户号添加');
                    if (!$hasPermission) {
                        return json_encode([
                            'status' => '2',
                            'message' => '没有-汇旺财商户号添加-权限'
                        ]);
                    }
                } else {
                    $hasPermission = $user->hasPermissionTo('汇旺财商户号修改');
                    if (!$hasPermission) {
                        return json_encode([
                            'status' => '2',
                            'message' => '没有-汇旺财商户号修改-权限'
                        ]);
                    }
                }

                if ($hwcpayMchId == '') {
                    if ($hwcpayStore) {
                        return json_encode([
                            'status' => '1',
                            'data' => [
                                'hwcpayMchId' => $hwcpayStore->mch_id, //门店号
                                'hwcpayMerchantNum' => $hwcpayStore->merchant_num, //商户号
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => '1',
                            'data' => []
                        ]);
                    }
                } else {
                    $config = new HwcPayConfigController();
                    $hwcpayConfig = $config->hwcpay_config($config_id);
                    if (!$hwcpayConfig) {
                        return json_encode([
                            'status' => '2',
                            'message' => '汇旺财支付配置不存在，请检查配置'
                        ]);
                    }

                    //读取商户的费率
                    $StorePayWay = StorePayWay::where('ways_type', $ways_type)
                        ->where('store_id', $store_id)
                        ->select('rate')
                        ->first();
                    if ($StorePayWay) {
                        $rate = $StorePayWay->rate;
                    } else {
                        return json_encode([
                            'status' => '2',
                            'message' => '商户通道费率未设置'
                        ]);
                    }

                    if ($hwcpayStore) {
                        $updateData = [
                            'mch_id' => $hwcpayMchId,
                            'merchant_num' => $hwcpayMerchantNum
                        ];
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        $hwcpayStore->update($updateData);
                    } else {
                        $hwcpayDataInsert = [
                            'store_id' => $store_id,
                            'config_id' => $config_id,
                            'mch_id' => $hwcpayMchId,
                            'merchant_num' => $hwcpayMerchantNum,
                            'status'=> 1
                        ];
                        HwcpayStore::create($hwcpayDataInsert);
                    }

                    //todo:默认第三方商户入网成功
                    $pay_status = 1;
                    $pay_status_desc = '开通成功';
                }

                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)
                    ->where('ways_type', 28001) //目前是一样的直接读取支付宝就行
                    ->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => '2',
                        'message' => '请联系代理商开启此通道'
                    ]);
                }

                //默认支付通道未注册成功
                // $data['store_id'] = $store_id;
                // $data['rate'] = $rate;
                // $data['status'] = $pay_status;
                // $data['status_desc'] = $pay_status_desc;
                // $data['company'] = 'hwcpay';
                // $return = $this->send_ways_data($data);
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'hwcpay')->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');

                return $return;
            }

            //银卡
            if (37999 < $ways_type && $ways_type < 38999) {
                $yinkaStore = YinkaStore::where('store_id', $store_id)->first();

                $yinkaUniqueId = $request->get('yinka_unique_id', '');
                $yinkaSn = $request->get('yinka_sn', '');
                $yinkaAccountId = $request->get('yinka_id', '');
                $yinkaKey = $request->get('yinka_key', '');
                $yinkaPriKey = $request->get('yinka_pri_key', '');
                if ($yinkaUniqueId == '') {
                    if ($yinkaStore) {
                        return json_encode([
                            'status' => '1',
                            'data' => [
                                'yinka_unique_id' => $yinkaStore->unique_id, //门店号
                                'yinka_sn' => $yinkaStore->sn, //商户号
                                'yinka_id' => $yinkaStore->account_id, //商户号
                                'yinka_key' => $yinkaStore->key, //商户号
                                'yinka_pri_key' => $yinkaStore->pri_key, //私钥
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => '1',
                            'data' => []
                        ]);
                    }
                } else {
                    if ($yinkaStore) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        $yinkaStore->unique_id = $yinkaUniqueId;
                        $yinkaStore->sn = $yinkaSn;
                        $yinkaStore->account_id = $yinkaAccountId;
                        $yinkaStore->key = $yinkaKey;
                        $yinkaStore->pri_key = $yinkaPriKey;
                        $yinkaStore->save();
                    } else {
                        $yinkaDataInsert = [
                            'store_id' => $store_id,
                            'config_id' => $config_id,
                            'unique_id' => $yinkaUniqueId,
                            'sn' => $yinkaSn,
                            'account_id' => $yinkaAccountId,
                            'key' => $yinkaKey,
                            'pri_key' => $yinkaPriKey,
                            'status'=> 1
                        ];
                        YinkaStore::create($yinkaDataInsert);
                    }

                    //todo:默认第三方商户入网成功
                    $pay_status = 1;
                    $pay_status_desc = '开通成功';
                }

                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)
                    ->where('ways_type', 38001) //目前是一样的直接读取支付宝就行
                    ->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => '2',
                        'message' => '请联系代理商开启此通道'
                    ]);
                }
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'hwcpay')->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');

                return $return;
            }

            //民生
            if (in_array($ways_type, [50001, 50002, 50003])) {
                $payStore = DB::table('cmbc_stores')->where('store_id', $store_id)->first();
                $mch_id = $request->get('cmbc_mch_id', '');
                $private_key = $request->get('cmbc_private_key', '');
                if ($mch_id == '') {
                    if ($payStore) {
                        return json_encode([
                            'status' => '1',
                            'data' => [
                                'cmbc_mch_id' => $payStore->mch_id, //商户号
                                'cmbc_private_key' => $payStore->private_key, //私钥
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => '1',
                            'data' => []
                        ]);
                    }
                } else {
                    $payConfig = new \App\Api\Controllers\Config\CmbcPayConfigController();
                    $config = $payConfig->config($config_id);
                    $private_key = 'MIIDEQIBATBHBgoqgRzPVQYBBAIBBgcqgRzPVQFoBDBXxqz6p5oeeRTzghAQjCAbgxrNXe2nmvnvVN4Sx5lRkIwB8HaUOOU4Z928RjgqfOcwggLBBgoqgRzPVQYBBAIBBIICsTCCAq0wggJRoAMCAQICBRUAWHRUMAwGCCqBHM9VAYN1BQAwJTELMAkGA1UEBhMCQ04xFjAUBgNVBAoMDUNGQ0EgU00yIE9DQTEwHhcNMjQxMDE0MDU1ODAwWhcNMjkxMDE0MDU1ODAwWjCBkjELMAkGA1UEBhMCQ04xEjAQBgNVBAoMCUNGQ0EgT0NBMTENMAsGA1UECwwEQ01CQzEZMBcGA1UECwwQT3JnYW5pemF0aW9uYWwtMTFFMEMGA1UEAww8MDMwNUA4OTEzNDAxMDBNQThOV0hNMlhIQOWuieW+veealuS7mOmAmuenkeaKgOaciemZkOWFrOWPuEAxMFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEjknkGt9BE2rhXLfpgu0ew6VepWTxU76ZviEW3Ggch+phOk2C5RAcBVOuQRaaBaD30a+8krgS2AMiOygVwaY6MKOB/TCB+jAfBgNVHSMEGDAWgBRck1ggWiRzVhAbZFAQ7OmnygdBETAJBgNVHRMEAjAAMEgGA1UdIARBMD8wPQYIYIEchu8qAQEwMTAvBggrBgEFBQcCARYjaHR0cDovL3d3dy5jZmNhLmNvbS5jbi91cy91cy0xNC5odG0wNwYDVR0fBDAwLjAsoCqgKIYmaHR0cDovL2NybC5jZmNhLmNvbS5jbi9TTTIvY3JsOTY5Ni5jcmwwCwYDVR0PBAQDAgPoMB0GA1UdDgQWBBTJEMeZOdpIMDuYqEJxwGmZmv0WzDAdBgNVHSUEFjAUBggrBgEFBQcDAgYIKwYBBQUHAwQwDAYIKoEcz1UBg3UFAANIADBFAiEAlgXPGVLNWW24CNSLQTSTXi+Q93UA2JyS0Ojxl2yE+1sCICZ87ZRbrco2BH3dr6jiETTAuPGzAKLo77UAeVfDjCCz';
                    if (!$config) {
                        return json_encode([
                            'status' => 2,
                            'message' => '民生银行支付配置不存在请检查配置'
                        ]);
                    }

                    if ($payStore) {
                        $updateData = [
                            'mch_id' => $mch_id,
                            'private_key' => $private_key
                        ];
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        DB::table('cmbc_stores')->where('store_id', $store_id)->update($updateData);
                    } else {
                        $insert = [
                            'store_id' => $store_id,
                            'config_id' => $config_id,
                            'mch_id' => $mch_id,
                            'private_key' => $private_key,
                        ];
                        DB::table('cmbc_stores')->insert($insert);
                    }
                }

                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)
                    ->where('ways_type', 50002) //目前是一样的直接读取支付宝就行
                    ->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => '2',
                        'message' => '请联系代理商开启此通道'
                    ]);
                }
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'cmbc')->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');

                return $return;
            }

            //银盛 TODO::银盛
            if (in_array($ways_type, [14001, 14002, 14004])) {
                $yinsheng_seller_id = $request->get('yinsheng_seller_id', '');
                $yinsheng_stores = DB::table('yinsheng_stores')->where(['store_id' => $store_id])->first();
                if ($yinsheng_seller_id == '') {
                    if ($yinsheng_stores) {
                        return json_encode([
                            'status' => '1',
                            'data' => [
                                'yinsheng_seller_id' => $yinsheng_stores->seller_id
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => '1',
                            'data' => []
                        ]);
                    }
                } else {
                    if ($yinsheng_stores) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        DB::table('yinsheng_stores')->where(['store_id' => $store_id])->update([
                            'seller_id' => $yinsheng_seller_id
                        ]);
                    } else {
                        DB::table('yinsheng_stores')->insert([
                            "config_id" => '1234',
                            "store_id" => $store_id,
                            "partner_id" => '826361673720106',
                            "seller_id" => $yinsheng_seller_id,
                            "seller_name" => $Store->store_name,
                            "business_code" => '00510030',
                            "status" => 1,
                        ]);
                    }
                    //todo:默认第三方商户入网成功
                    $pay_status = 1;
                    $pay_status_desc = '开通成功';
                }
                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)->where('ways_type', 14001)->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => '2',
                        'message' => '请联系代理商开启此通道'
                    ]);
                }
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'yinsheng')->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');
                return $return;
            }


            //民生
            if (in_array($ways_type, [50001, 50002, 50003])) {
                $payStore = DB::table('cmbc_stores')->where('store_id', $store_id)->first();
                $mch_id = $request->get('cmbc_mch_id', '');
                $private_key = $request->get('cmbc_private_key', '');
                if ($mch_id == '') {
                    if ($payStore) {
                        return json_encode([
                            'status' => '1',
                            'data' => [
                                'cmbc_mch_id' => $payStore->mch_id, //商户号
                                'cmbc_private_key' => $payStore->private_key, //私钥
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => '1',
                            'data' => []
                        ]);
                    }
                } else {
                    $payConfig = new \App\Api\Controllers\Config\CmbcPayConfigController();
                    $config = $payConfig->config($config_id);

                    if (!$config) {
                        return json_encode([
                            'status' => 2,
                            'message' => '民生银行支付配置不存在请检查配置'
                        ]);
                    }

                    if ($payStore) {

                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }


                        $updateData = [
                            'mch_id' => $mch_id,
                            'private_key' => $private_key
                        ];
                        DB::table('cmbc_stores')->where('store_id', $store_id)->update($updateData);
                    } else {
                        $insert = [
                            'store_id' => $store_id,
                            'config_id' => $config_id,
                            'mch_id' => $mch_id,
                            'private_key' => $private_key,
                        ];
                        DB::table('cmbc_stores')->insert($insert);
                    }
                }

                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)
                    ->where('ways_type', 50002) //目前是一样的直接读取支付宝就行
                    ->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => '2',
                        'message' => '请联系代理商开启此通道'
                    ]);
                }
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'cmbc')->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');

                return $return;
            }

            //东莞
            if (in_array($ways_type, [40001, 40002, 40003])) {
                $dongguanbank_stores = DB::table('dongguanbank_stores')
                    ->where(['store_id' => $store_id])
                    ->select([
                        'id',
                        DB::raw("mch_id as dongguan_mchtNo"),
                        DB::raw("op_device_id as dongguan_termNo"),
                        DB::raw("private_key as dongguan_private_key"),
                    ])
                    ->first();
                if ($MerchantId == '') {
                    if ($dongguanbank_stores) {
                        return json_encode([
                            'status' => 1,
                            'data' => $dongguanbank_stores
                        ]);
                    } else {
                        return json_encode([
                            'status' => 1,
                            'data' => []
                        ]);
                    }
                } else {
                    $in = [
                        'store_id' => $store_id,
                        'config_id' => $config_id,
                        'mch_id' => $dongguan_mchtNo,
                        'op_device_id' => $dongguan_termNo,
                        'private_key' => $dongguan_private_key,
                    ];
                    if ($dongguanbank_stores) {
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        DB::table('dongguanbank_stores')->where(['store_id' => $store_id])->update($in);
                    } else {
                        DB::table('dongguanbank_stores')->insert($in);
                    }
                    DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'dongguan')->update(array('status' => 1, 'status_desc' => '开通成功'));
                    $return = array('status' => 1, 'message' => '修改成功');
                    return $return;
                }
            }


            //gtxy
            if (in_array($ways_type, [62001,62002,62004,62011,62012,62014])) {
                //gtxy_v2
                if(in_array($ways_type,[62001,62002,62004])){
                    $tableName="common_mch_stores";
                    $company="gtxy";
                }else{
                    $tableName="common_mch_stores_v2";
                    $company="gtxy_v2";
                }
                $payStore = DB::table($tableName)->where('store_id', $store_id)
                    ->where("company",$company)
                    ->first();
                $mch_id = $request->get('mch_id', '');
                if ($mch_id == '') {
                    if ($payStore) {
                        return json_encode([
                            'status' => '1',
                            'data' => [
                                'mch_id' => $payStore->mch_no, //商户号
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => '1',
                            'data' => []
                        ]);
                    }
                } else {
                    $config = DB::table('common_configs')->where('company', "gtxy")
                        ->first();
                    if (!$config) {
                        return json_encode([
                            'status' => 2,
                            'message' => '国通支付配置不存在请检查配置'
                        ]);
                    }

                    if ($payStore) {

                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        DB::table($tableName)->where([
                            "store_id"=>$store_id,
                            "company"=>$company,
                        ])->update([
                            "mch_no"=> $mch_id
                        ]);
                    } else {
                        DB::table($tableName)->insert([
                            "mch_no"=>$mch_id,
                            "store_id"=>$store_id,
                            "company"=>$company,
                            "created_at"=>date("Y-m-d H:i:s",time()),
                            "updated_at"=>date("Y-m-d H:i:s",time()),
                        ]);
                    }
                }
                //费率 默认商户的费率为代理商的费率
                if($tableName=="common_mch_stores"){
                    $UserRate = UserRate::where('user_id', $Store->user_id)
                        ->where('ways_type', 62002) //目前是一样的直接读取支付宝就行
                        ->first();
                }else{
                    $UserRate = UserRate::where('user_id', $Store->user_id)
                        ->where('ways_type', 62012) //目前是一样的直接读取支付宝就行
                        ->first();
                }
                if (!$UserRate) {
                    return json_encode([
                        'status' => '2',
                        'message' => '请联系代理商开启此通道'
                    ]);
                }
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', $company)->update(array('status'=>1,'status_desc'=>'开通成功'));
                return json_encode([
                    'status' => '1',
                    'message' => '修改成功'
                ]);
            }

            //民生联合收单
            if (in_array($ways_type, [70001, 70002, 70003])) {
                $payStore = DB::table('cmbcunion_stores')->where('store_id', $store_id)->first();
                $mch_id = $request->get('mch_id', '');
                if ($mch_id == '') {
                    if ($payStore) {
                        return json_encode([
                            'status' => '1',
                            'data' => [
                                'cmbc_mch_id' => $payStore->mch_id, //商户号
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => '1',
                            'data' => []
                        ]);
                    }
                } else {
                    $payConfig = new \App\Api\Controllers\Config\CmbcUnionPayConfigController();
                    $config = $payConfig->config($config_id);
                    if (!$config) {
                        return json_encode([
                            'status' => 2,
                            'message' => '民生银行支付配置不存在请检查配置'
                        ]);
                    }

                    if ($payStore) {
                        $updateData = [
                            'mch_id' => $mch_id
                        ];
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        DB::table('cmbcunion_stores')->where('store_id', $store_id)->update($updateData);
                    } else {
                        $insert = [
                            'store_id' => $store_id,
                            'config_id' => $config_id,
                            'mch_id' => $mch_id,
                        ];
                        DB::table('cmbcunion_stores')->insert($insert);
                    }
                }

                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)
                    ->where('ways_type', 70002) //目前是一样的直接读取支付宝就行
                    ->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => '2',
                        'message' => '请联系代理商开启此通道'
                    ]);
                }
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'cmbcunion')->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');

                return $return;
            }

            //拉卡拉
            if (in_array($ways_type, [90001, 90002, 90003])) {
                $payStore = DB::table('lakala_stores')->where('store_id', $store_id)->first();
                $mch_id = $request->get('mch_id', '');
                $key   = $request->get('key', '');
                if ($mch_id == '') {
                    if ($payStore) {
                        return json_encode([
                            'status' => '1',
                            'data' => [
                                'mch_id' => $payStore->mch_id, //商户号
                                'key' => $payStore->key, //商户号
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => '1',
                            'data' => []
                        ]);
                    }
                } else {
                    $payConfig = new \App\Api\Controllers\Config\LakalaPayConfigController();
                    $config = $payConfig->config($config_id);
                    if (!$config) {
                        return json_encode([
                            'status' => 2,
                            'message' => '拉卡拉支付配置不存在请检查配置'
                        ]);
                    }

                    if ($payStore) {
                        $updateData = [
                            'mch_id' => $mch_id,
                            'key' => $key
                        ];
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        DB::table('lakala_stores')->where('store_id', $store_id)->update($updateData);
                    } else {
                        $insert = [
                            'store_id' => $store_id,
                            'config_id' => $config_id,
                            'mch_id' => $mch_id,
                            'key' => $key
                        ];
                        DB::table('lakala_stores')->insert($insert);
                    }
                }

                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)
                    ->where('ways_type', 90002) //目前是一样的直接读取支付宝就行
                    ->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => '2',
                        'message' => '请联系代理商开启此通道'
                    ]);
                }
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'lakala')->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');

                return $return;
            }


            //拉卡拉
            if (in_array($ways_type, [91001, 91002, 91003])) {
                $payStore = DB::table('lakalaunion_stores')->where('store_id', $store_id)->first();
                $mch_id = $request->get('mch_id', '');
                $key   = $request->get('key', '');
                if ($mch_id == '') {
                    if ($payStore) {
                        return json_encode([
                            'status' => '1',
                            'data' => [
                                'mch_id' => $payStore->mch_id, //商户号
                                'key' => $payStore->key, //商户号
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => '1',
                            'data' => []
                        ]);
                    }
                } else {
                    $payConfig = new \App\Api\Controllers\Config\LakalaUnionPayConfigController();
                    $config = $payConfig->config($config_id);
                    if (!$config) {
                        return json_encode([
                            'status' => 2,
                            'message' => '拉卡拉支付配置不存在请检查配置'
                        ]);
                    }

                    if ($payStore) {
                        $updateData = [
                            'mch_id' => $mch_id,
                            'key' => $key
                        ];
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        DB::table('lakalaunion_stores')->where('store_id', $store_id)->update($updateData);
                    } else {
                        $insert = [
                            'store_id' => $store_id,
                            'config_id' => $config_id,
                            'mch_id' => $mch_id,
                            'key' => $key
                        ];
                        DB::table('lakalaunion_stores')->insert($insert);
                    }
                }

                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)
                    ->where('ways_type', 91002) //目前是一样的直接读取支付宝就行
                    ->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => '2',
                        'message' => '请联系代理商开启此通道'
                    ]);
                }
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'lakalaunion')->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');

                return $return;
            }

            if (in_array($ways_type, [140001, 140002, 140003])) {
                $payStore = DB::table('leshuaunion_stores')->where('store_id', $store_id)->first();
                $mch_id = $request->get('mch_id', '');
//                $key   = $request->get('key', '');
                if ($mch_id == '') {
                    if ($payStore) {
                        return json_encode([
                            'status' => '1',
                            'data' => [
                                'mch_id' => $payStore->mch_id, //商户号
//                                'key' => $payStore->key, //商户号
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => '1',
                            'data' => []
                        ]);
                    }
                } else {
                    $payConfig = new \App\Api\Controllers\Config\LeshuaUnionPayConfigController();
                    $config = $payConfig->config($config_id);
                    if (!$config) {
                        return json_encode([
                            'status' => 2,
                            'message' => '乐刷支付配置不存在请检查配置'
                        ]);
                    }

                    if ($payStore) {
                        $updateData = [
                            'mch_id' => $mch_id,
//                            'key' => $key
                        ];
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        DB::table('leshuaunion_stores')->where('store_id', $store_id)->update($updateData);
                    } else {
                        $insert = [
                            'store_id' => $store_id,
                            'config_id' => $config_id,
                            'mch_id' => $mch_id,
//                            'key' => $key
                        ];
                        DB::table('leshuaunion_stores')->insert($insert);
                    }
                }

                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)
                    ->where('ways_type', 140001) //目前是一样的直接读取支付宝就行
                    ->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => '2',
                        'message' => '请联系代理商开启此通道'
                    ]);
                }
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'leshuaunion')->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');

                return $return;
            }
            //联厚支付
            if (in_array($ways_type, [80001, 80002, 80003])) {
                $payStore = DB::table('lianhou_stores')->where('store_id', $store_id)->first();
                $mch_id = $request->get('mch_id', '');
                $term_no   = $request->get('term_no', '');
                if ($mch_id == '') {
                    if ($payStore) {
                        return json_encode([
                            'status' => '1',
                            'data' => [
                                'mch_id' => $payStore->mch_id, //商户号
                                'term_no' => $payStore->term_no, //商户号
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => '1',
                            'data' => []
                        ]);
                    }
                } else {
                    $payConfig = new \App\Api\Controllers\Config\LianhouPayConfigController();
                    $config = $payConfig->config($config_id);
                    if (!$config) {
                        return json_encode([
                            'status' => 2,
                            'message' => '联厚支付配置不存在请检查配置'
                        ]);
                    }

                    if ($payStore) {
                        $updateData = [
                            'mch_id' => $mch_id,
                            'term_no' => $term_no
                        ];
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        DB::table('lianhou_stores')->where('store_id', $store_id)->update($updateData);
                    } else {
                        $insert = [
                            'store_id' => $store_id,
                            'config_id' => $config_id,
                            'mch_id' => $mch_id,
                            'term_no' => $term_no
                        ];
                        DB::table('lianhou_stores')->insert($insert);
                    }
                }

                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)
                    ->where('ways_type', 80002) //目前是一样的直接读取支付宝就行
                    ->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => '2',
                        'message' => '请联系代理商开启此通道'
                    ]);
                }
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'lianhou')->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');

                return $return;
            }

            //中国亚大
            if (in_array($ways_type, [130001, 130002, 130003])) {
                $payStore = DB::table('zgyd_stores')->where('store_id', $store_id)->first();
                $mch_id = $request->get('mch_id', '');
                $pos_no   = $request->get('pos_no', '');
                $pri_key   = $request->get('pri_key', '');
                $sales_no   = $request->get('sales_no', '');
                if ($mch_id == '') {
                    if ($payStore) {
                        return json_encode([
                            'status' => '1',
                            'data' => [
                                'mch_id' => $payStore->mch_id, //商户号
                                'pos_no' => $payStore->pos_no, //商户号
                                'pri_key' => $payStore->pri_key, //商户号
                                'sales_no' => $payStore->sales_no, //商户号
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => '1',
                            'data' => []
                        ]);
                    }
                } else {
                    $payConfig = new \App\Api\Controllers\Zgyd\LogicController();
                    $config = $payConfig->config($config_id);
                    if (!$config) {
                        return json_encode([
                            'status' => 2,
                            'message' => '中国亚大配置不存在请检查配置'
                        ]);
                    }

                    if ($payStore) {
                        $updateData = [
                            'mch_id' => $mch_id,
                            'pos_no' => $pos_no,
                            'pri_key' => $pri_key,
                            'sales_no' => $sales_no,
                        ];
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        DB::table('zgyd_stores')->where('store_id', $store_id)->update($updateData);
                    } else {
                        $insert = [
                            'pos_no' => $pos_no,
                            'pri_key' => $pri_key,
                            'sales_no' => $sales_no,
                            'store_id' => $store_id,
                            'config_id' => $config_id,
                            'mch_id' => $mch_id
                        ];
                        DB::table('zgyd_stores')->insert($insert);
                    }
                }

                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)
                    ->where('ways_type', 130002) //目前是一样的直接读取支付宝就行
                    ->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => '2',
                        'message' => '请联系代理商开启此通道'
                    ]);
                }
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'zgyd')->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');

                return $return;
            }

            //中国嘉利
            if (in_array($ways_type, [120001, 120002, 120003])) {
                $payStore = DB::table('zgjl_stores')->where('store_id', $store_id)->first();
                $mch_id = $request->get('mch_id', '');
                $pos_no   = $request->get('pos_no', '');
                $key   = $request->get('key', '');
                if ($mch_id == '') {
                    if ($payStore) {
                        return json_encode([
                            'status' => '1',
                            'data' => [
                                'mch_id' => $payStore->mch_id, //商户号
                                'pos_no' => $payStore->pos_no, //商户号
                                'key' => $payStore->key, //商户号
                            ]
                        ]);
                    } else {
                        return json_encode([
                            'status' => '1',
                            'data' => []
                        ]);
                    }
                } else {
                    $payConfig = new \App\Api\Controllers\Zgjl\LogicController();
                    $config = $payConfig->config($config_id);
                    if (!$config) {
                        return json_encode([
                            'status' => 2,
                            'message' => '中国嘉利配置不存在请检查配置'
                        ]);
                    }

                    if ($payStore) {
                        $updateData = [
                            'mch_id' => $mch_id,
                            'pos_no' => $pos_no,
                            'key' => $key,
                        ];
                        $num=OrderService::countOrderNum((string)$store_id,(string)$ways_type   , [
                            "user_id"=>$token->user_id
                        ]);
                        if($num>0){
                            return json_encode([
                                'status' => 2,
                                'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                            ]);
                        }
                        DB::table('zgjl_stores')->where('store_id', $store_id)->update($updateData);
                    } else {
                        $insert = [
                            'pos_no' => $pos_no,
                            'key' => $key,
                            'store_id' => $store_id,
                            'config_id' => $config_id,
                            'mch_id' => $mch_id
                        ];
                        DB::table('zgjl_stores')->insert($insert);
                    }
                }

                //费率 默认商户的费率为代理商的费率
                $UserRate = UserRate::where('user_id', $Store->user_id)
                    ->where('ways_type', 120002) //目前是一样的直接读取支付宝就行
                    ->first();
                if (!$UserRate) {
                    return json_encode([
                        'status' => '2',
                        'message' => '请联系代理商开启此通道'
                    ]);
                }
                DB::table('store_pay_ways')->where('store_id', $store_id)->where('company', 'zgjl')->update(array('status'=>1,'status_desc'=>'开通成功'));
                $return = array('status'=>1,'message'=>'修改成功');

                return $return;
            }


            $storeWaysDesc = DB::table('store_ways_desc')->where('ways_type', $ways_type)->first();

            //判断是否有表
            if (\Illuminate\Support\Facades\Schema::hasTable($storeWaysDesc->company.'_stores')) {
                return $this->open_ways_aggs($request, $Store, $storeWaysDesc, $token);
            }

            return json_encode([
                'status' => '1',
                'message' => '保存成功'
            ]);
        } catch (\Exception $exception) {
            return json_encode([
                'status' => -1,
                'message' => $exception->getMessage().' | '.$exception->getFile().' | '.$exception->getLine()
            ]);
        }
    }

    public function open_ways_aggs($request, $Store, $storeWaysDesc, $token)
    {
        $tables = $storeWaysDesc->company.'_stores';
        $payStore = DB::table($tables)->where('store_id', $Store->store_id)->first();
        $mch_id = $request->get('mch_id', '');
        if ($mch_id == '') {
            if ($payStore) {
                return json_encode([
                    'status' => '1',
                    'data' => [
                        'mch_id' => $payStore->mch_id, //商户号
                    ]
                ]);
            } else {
                return json_encode([
                    'status' => '1',
                    'data' => []
                ]);
            }
        } else {
            $controller = ucfirst($storeWaysDesc->company).'PayConfigController';
            $payConfigController =  "\\App\\Api\\Controllers\\Config\\$controller";

            $config = (new $payConfigController)->config($Store->config_id);
            if (!$config) {
                return json_encode([
                    'status' => 2,
                    'message' => $storeWaysDesc->ways_desc.'配置不存在请检查配置'
                ]);
            }
            if ($payStore) {
                $num=OrderService::countOrderNum((string) $Store->store_id,(string) $storeWaysDesc->ways_type   , [
                    "user_id"=>$token->user_id
                ]);
                if($num>0){
                    return json_encode([
                        'status' => 2,
                        'message' => '近10天内存在支付订单，无法修改，请联系开发人员',
                    ]);
                }
                $updateData = [
                    'mch_id' => $mch_id,
                    'updated_at' => date('Y-m-d H:i:s')
                ];
                DB::table($tables)->where('store_id', $Store->store_id)->update($updateData);
            } else {
                $insert = [
                    'store_id' => $Store->store_id,
                    'config_id' => $Store->config_id,
                    'mch_id' => $mch_id,
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s')

                ];
                DB::table($tables)->insert($insert);
            }
        }
        //费率 默认商户的费率为代理商的费率
        $UserRate = UserRate::where('user_id', $Store->user_id)
            ->where('ways_type', $storeWaysDesc->ways_type) //目前是一样的直接读取支付宝就行
            ->first();
        if (!$UserRate) {
            return json_encode([
                'status' => '2',
                'message' => '请联系代理商开启此通道'
            ]);
        }
        DB::table('store_pay_ways')->where('store_id', $Store->store_id)->where('company', $storeWaysDesc->company)->update(array('status'=>1,'status_desc'=>'开通成功'));
        return array('status'=>1,'message'=>'修改成功');
    }

    //pc端开通通道
    public function get_sub_merchant(Request $request)
    {
        try {
            $token = $this->parseToken();
            $pay_status = $request->get('pay_status', '1');
            $pay_status_desc = $request->get('pay_status_desc', '开通成功');
            $store_id = $request->get('store_id');
            $ways_type = $request->get('ways_type', '');

            //支付宝
            $alipay_store_id = $request->get('alipay_store_id', $store_id);
            $out_store_id = $request->get('out_store_id', $store_id);

            if ($alipay_store_id == "") {
                $alipay_store_id = "";
            }

            if ($out_store_id == "") {
                $out_store_id = "";
            }

            //翼支付
            $wing_institutionCode=$request->get('wing_institutionCode', '');
            $wing_merchantNo=$request->get('wing_merchantNo', '');


            $Store = Store::where('store_id', $store_id)
                ->first();
            if (!$Store) {
                return json_encode([
                    'status' => 2,
                    'message' => '门店不存在',
                ]);
            }

            $config_id = $Store->config_id;
            $store_name = $Store->store_name;
            $store_pid = $Store->pid;

            //费率 默认商户的费率为代理商的费率
            $UserRate = UserRate::where('user_id', $Store->user_id)
                ->where('ways_type', $ways_type) //目前是一样的直接读取支付宝就行
                ->first();
            if (!$UserRate) {
                return json_encode([
                    'status' => 2,
                    'message' => '请联系代理商开启此通道',
                ]);
            }

            $rate = $UserRate->store_all_rate;
            //翼支付
            if ('30050' == $ways_type || '30051' == $ways_type || '30052' == $ways_type || '30053' == $ways_type) {
                $wing=DB::table('wing_stores')->where(['store_id' => $store_id])->first();
                $return_data = [
                    'traceLogId' => 'wing'.time(),
                    'requestSystem' => 'AHHFWHPAY',
                    'agentMerchantCode' => '3178035630163868',
                    'merchantCode' => $wing->merchantNo,
                ];
                $obj = new \App\Api\Controllers\lianfu\PayController();
                $return = $obj->get_sub_merchant($return_data);
                $return=json_decode($return,true);
                error_log(date('Y-m-d H:i:s')." 【翼支付】： ".json_encode($return)."\n\n", 3, "/www/wwwlogs/wft/". 'prod-lianfu'.date('Ymd').".log");
                foreach($return as $k=>$v){
                   if($v['paymentChannel']=='NUCC-WECHAT'){
                       $weixin_sub=$v['subMerchantCode'];
                   }else if($v['paymentChannel']=='NUCC-ALIPAY'){
                       $alipay_sub=$v['subMerchantCode'];
                   }else if($v['paymentChannel']=='UP-WECHANT'){
                       $weixin_sub=$v['subMerchantCode'];
                   }else if($v['paymentChannel']=='UP-ALIPAY'){
                       $alipay_sub=$v['subMerchantCode'];
                   }else if($v['paymentChannel']=='UP-QUICKPASS'){
                       $uni_sub=$v['subMerchantCode'];
                   }else if($v['paymentChannel']=='BESTPAY-WECHAT'){
                       $weixin_sub=$v['subMerchantCode'];
                   }else if($v['paymentChannel']=='BESTPAY-ALIPAY'){
                       $alipay_sub=$v['subMerchantCode'];
                   }
                }
                return json_encode([
                    'status' => '1',
                    'message' => '获取成功',
                    'weixin_sub' => $weixin_sub?$weixin_sub:'未开通',
                    'alipay_sub' => $alipay_sub?$alipay_sub:'未开通',
                    'uni_sub' => $uni_sub?$uni_sub:'未开通',
                ]);
            }else  if(in_array($ways_type,[21001,21002,21004])){
                $eStore=EasypayStore::query()->where(["store_id"=>$store_id])->first();
                if(empty($eStore)){
                    return json_encode([
                        'status' => '2',
                        'message' => '获取失败'
                    ]);
                }
                return json_encode([
                    'status' => '1',
                    'message' => '获取成功',
                    'weixin_sub' => $eStore->wechatId,
                    'alipay_sub' => $eStore->alipayId,
                    'uni_sub' => "",
                ]);
            }else if(in_array($ways_type,[61001,61002,61004])){
                $eStore=EasypayaStore::query()->where("store_id",$Store->store_id)->first();
                if(empty($eStore)){
                    return json_encode([
                        'status' => '2',
                        'message' => '获取失败'
                    ]);
                }
                return json_encode([
                    'status' => '1',
                    'message' => '获取成功',
                    'weixin_sub' => $eStore->wechatId,
                    'alipay_sub' => $eStore->alipayId,
                    'uni_sub' => "",
                ]);
            }else{
                return json_encode([
                    'status' => '2',
                    'message' => '该通道暂不支持获取'
                ]);
            }
        } catch (\Exception $exception) {
            return json_encode([
                'status' => -1,
                'message' => $exception->getMessage().' | '.$exception->getFile().' | '.$exception->getLine()
            ]);
        }
    }


    //修改费率
    public function send_ways_data($data)
    {
        try {
            $user = $this->parseToken();

            $all_pay_ways = DB::table('store_ways_desc')->where('company', $data['company'])->get();
            foreach ($all_pay_ways as $k => $v) {
                $gets = StorePayWay::where('store_id', $data['store_id'])
                    ->where('ways_source', $v->ways_source)
                    ->get();
                $count = count($gets);
                $ways = StorePayWay::where('store_id', $data['store_id'])->where('ways_type', $v->ways_type)->first();
                try {
                    //开启事务
                    DB::beginTransaction();
                    $data = [
                        'store_id' => $data['store_id'],
                        'ways_type' => $v->ways_type,
                        'company' => $v->company,
                        'ways_source' => $v->ways_source,
                        'ways_desc' => $v->ways_desc,
                        'sort' => ($count + 1),
                        'rate' => $data['rate'],
                        'settlement_type' => $v->settlement_type,
                        'status' => $data['status'],
                        'status_desc' => $data['status_desc'],
                    ];
                    if ($ways) {
                        $ways->update([
                            'status' => $data['status'],
                            'rate' => $data['rate'],
                            'status_desc' => $data['status_desc'],
                            'pcredit' => '02',
                        ]);
                        $ways->save();
                    } else {
                        StorePayWay::create($data);
                    }
                    DB::commit();

                    // $this->operation_log( $user->user_id, 3, '门店修改费率: 门店id: ' . $data['store_id'] . '修改为费率: ' . $data['rate'] ); // 操作日志

                } catch (\Exception $ex) {
                    Log::info('user-store-入库通道修改费率-error');
                    Log::info($ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine());
                    DB::rollBack();
                    return [
                        'status' => 2,
                        'message' => '通道入库费率更新失败',
                    ];
                }
            }

            return [
                'status' => 1,
                'message' => '修改成功',
            ];
        } catch (\Exception $e) {
//            dd($e);
            Log::info('user-store-入库通道22-error');
            Log::info($e->getMessage().' | '.$e->getFile().' | '.$e->getLine());
            return [
                'status' => 2,
                'message' => '通道入库更新失败',
            ];
        }
    }


    /**
     * 添加分店
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function add_sub_store(Request $request)
    {
        try {
            $user = $this->parseToken();
            $store_name = $request->get('store_name', '');
            // $province_code = $request->get('province_code', '');
            // $city_code = $request->get('city_code', '');
            // $area_code = $request->get('area_code', '');
            // $store_address = $request->get('store_address', '');
            $pid = $request->get('id', '');


            if ($store_name == "") {
                $this->status = 2;
                $this->message = '店铺名称必须填写';
                return $this->format();
            }

            // if ($area_code == "") {
            //     $this->status = 2;
            //     $this->message = '地区编号必须填写';
            //     return $this->format();
            // }
            $pid_store = Store::where('id', $pid)->first();
            if (!$pid_store) {
                return json_encode([
                    'status' => 2,
                    'message' => '总门店不存在'
                ]);
            }

            $User = User::where('id', $pid_store->user_id)
                ->select('is_delete')
                ->first();
            if (!$User) {
                return json_encode([
                    'status' => 2,
                    'message' => '业务员被删除无法添加门店'
                ]);
            }

            if ($User->is_delete) {
                return json_encode([
                    'status' => 2,
                    'message' => '业务员被删除无法添加门店'
                ]);
            }

            if ($pid_store->pid) {
                return json_encode([
                    'status' => 2,
                    'message' => '分店暂不允许再创建分店'
                ]);
            }

            $store_id = date('YmdHis', time()) . rand(10000, 99999);
            $data = [
                'store_id' => $store_id,
                'config_id' => $pid_store->config_id,
                'merchant_id' => $pid_store->merchant_id,
                'pid' => $pid_store->id,
                'store_name' => $store_name,
                'store_short_name' => $store_name,
                'province_code' => '340000',
                'city_code' => '340100',
                'area_code' => '340103',
                'province_name' => $this->city_name('340000'),
                'city_name' => $this->city_name('340100'),
                'area_name' => $this->city_name('340103'),
                'store_address' => '',
                'user_id' => $pid_store->user_id,
                'user_pid' => $pid_store->user_pid,
                'store_type' => $pid_store->store_type,
                'store_type_name' => $pid_store->store_type_name,
                'category_id' => $pid_store->category_id,
                'category_name' => $pid_store->category_name,
            ];


            //开启事务
            try {
                DB::beginTransaction();

                Store::create($data);
                MerchantStore::create([
                    'store_id' => $store_id,
                    'merchant_id' => $pid_store->merchant_id
                ]);


                StoreBank::create([
                    'store_id' => $store_id,
                ]);
                StoreImg::create([
                    'store_id' => $store_id,
                ]);

                DB::commit();
            } catch (\Exception $e) {
                DB::rollBack();
                return json_encode([
                    'status' => '-1',
                    'message' => $e->getMessage() .' | '. $e->getFile() .' | '. $e->getLine()
                ]);
            }

            $this->status = 1;
            $this->message = '分店添加成功';
            $data = [
                'store_id' => $store_id,
                'pid' => $pid,
            ];

            return $this->format($data);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }

    /**
     * 添加分店
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function add_sub_store1(Request $request)
    {
        try {
            $user = $this->parseToken();
            $store_name = $request->get('store_name', '');
            $province_code = $request->get('province_code', '');
            $city_code = $request->get('city_code', '');
            $area_code = $request->get('area_code', '');
            $store_address = $request->get('store_address', '');
            $pid = $request->get('pid', '');


            if ($store_name == "") {
                $this->status = 2;
                $this->message = '店铺名称必须填写';
                return $this->format();
            }

            if ($area_code == "") {
                $this->status = 2;
                $this->message = '地区编号必须填写';
                return $this->format();
            }
            $pid_store = Store::where('id', $pid)->first();
            if (!$pid_store) {
                return json_encode([
                    'status' => 2,
                    'message' => '总门店不存在'
                ]);
            }

            $User = User::where('id', $pid_store->user_id)
                ->select('is_delete')
                ->first();
            if (!$User) {
                return json_encode([
                    'status' => 2,
                    'message' => '业务员被删除无法添加门店'
                ]);
            }

            if ($User->is_delete) {
                return json_encode([
                    'status' => 2,
                    'message' => '业务员被删除无法添加门店'
                ]);
            }

            if ($pid_store->pid) {
                return json_encode([
                    'status' => 2,
                    'message' => '分店暂不允许再创建分店'
                ]);
            }

            $store_id = date('YmdHis', time()) . rand(10000, 99999);
            $data = [
                'store_id' => $store_id,
                'config_id' => $pid_store->config_id,
                'merchant_id' => $pid_store->merchant_id,
                'pid' => $pid_store->id,
                'store_name' => $store_name,
                'store_short_name' => $store_name,
                'province_code' => $province_code,
                'city_code' => $city_code,
                'area_code' => $area_code,
                'province_name' => $this->city_name($province_code),
                'city_name' => $this->city_name($city_code),
                'area_name' => $this->city_name($area_code),
                'store_address' => $store_address,
                'user_id' => $pid_store->user_id,
                'user_pid' => $pid_store->user_pid,
                'store_type' => $pid_store->store_type,
                'store_type_name' => $pid_store->store_type_name,
                'category_id' => $pid_store->category_id,
                'category_name' => $pid_store->category_name,
            ];


            //开启事务
            try {
                DB::beginTransaction();

                Store::create($data);
                MerchantStore::create([
                    'store_id' => $store_id,
                    'merchant_id' => $pid_store->merchant_id
                ]);


                StoreBank::create([
                    'store_id' => $store_id,
                ]);
                StoreImg::create([
                    'store_id' => $store_id,
                ]);

                DB::commit();
            } catch (\Exception $e) {
                DB::rollBack();
                return json_encode([
                    'status' => '-1',
                    'message' => $e->getMessage() .' | '. $e->getFile() .' | '. $e->getLine()
                ]);
            }

            $this->status = 1;
            $this->message = '分店添加成功';
            $data = [
                'store_id' => $store_id,
            ];

            return $this->format($data);
        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }


    public function store_pay_qr(Request $request)
    {
        try {
            $user = $this->parseToken();
            $store_id = $request->get('store_id', '');
            $ways_type = $request->get('ways_type', '');

            $this->status = 1;
            $this->message = '数据返回成功';
            $data = [
                'store_pay_qr' => env("APP_URL").'/qr?store_id=' . $store_id,
                'store_id' => $store_id,
                'ways_type' => $ways_type
            ];

            return $this->format($data);
        } catch (\Exception $ex) {
            $this->status = -1;
            $this->message = $ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine();
            return $this->format();
        }
    }

    public function store_pay_qr1(Request $request)
    {
        try {
            $user = $this->parseToken();
            $store_id = $request->get('store_id', '');
            $ways_type = $request->get('ways_type', '');

            $this->status = 1;
            $this->message = '数据返回成功';
            $data = [
                'store_pay_qr' => url('/merch_qr?store_id=' . $store_id),
                'store_id' => $store_id,
                'ways_type' => $ways_type
            ];

            return $this->format($data);
        } catch (\Exception $ex) {
            $this->status = -1;
            $this->message = $ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine();
            return $this->format();
        }
    }

    public function user_qrpay(Request $request)
    {
        try {
            $user = $this->parseToken();

            $this->status = 1;
            $this->message = '数据返回成功';
            $data = [
                'store_pay_qr' => url('/user_qr?user_id=' . $user->user_id)
            ];

            return $this->format($data);
        } catch (\Exception $ex) {
            $this->status = -1;
            $this->message = $ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine();
            return $this->format();
        }
    }


    /**
     * 支付宝授权
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|string
     */
    public function alipay_auth(Request $request)
    {
        try {
            $user = $this->parseToken();
            $store_id = $request->get('store_id');
            $store = Store::where('store_id', $store_id)
                ->select('id', 'merchant_id', 'config_id')
                ->first();
            if (!$store) {
                return [
                    'status' => 2,
                    'message' => '门店不存在',
                ];
            }


            //第一次授权
            $AlipayAppOauthUsers = AlipayAppOauthUsers::where('store_id', $store_id)
                ->select('id')
                ->first();

            $user = User::where('id', $user->user_id)->first();
            if ($AlipayAppOauthUsers) {
                //看下是否有修改权限
                $hasPermission = $user->hasPermissionTo('支付宝授权');

                if (!$hasPermission) {
                    return json_encode(['status' => 2, 'message' => '没有权限']);
                }
            } else {
                //看下是否有添加权限
                $hasPermission = $user->hasPermissionTo('支付宝授权修改');
                if (!$hasPermission) {
                    return json_encode(['status' => 2, 'message' => '没有权限修改']);
                }
            }

            $data = [
                'redirect_url' => 'alipays://platformapi/startapp?appId=20000067&url=' . url('/merchant/appAlipay?store_id=' . $store_id . '&merchant_id=' . $store->merchant_id . "&config_id=" . $store->config_id),
                'qr_url' => url('/merchant/appAlipay?store_id=' . $store_id . '&merchant_id=' . $store->merchant_id . "&config_id=" . $store->config_id)
            ];
            $this->status = 1;
            $this->message = '数据请求成功';
            return $this->format($data);

        } catch (\Exception $exception) {
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }


    //审核门店
    public function check_store(Request $request)
    {
        try {
            $user = $this->parseToken();
            $store_id = $request->get('store_id', '');
            $status = $request->get('status', '1');
            $status_desc = $request->get('status_desc', '');

            $Store = Store::where('store_id', $store_id)->first();
            if (!$Store) {
                return json_encode([
                    'status' => 2,
                    'message' => '门店不存在'
                ]);
            }

            //地址转经纬度
            if (env('LBS_KEY')) {
                if (isset($Store->city_name) && isset($Store->city_code)) {
                    $address = '';
                    if (isset($Store->province_name) && isset($Store->province_code)) $address = $Store->province_name;
                    if (isset($Store->city_name) && isset($Store->city_code)) $address .= $Store->city_name;
                    if (isset($Store->area_name) && isset($Store->area_code)) $address .= $Store->area_name;
                    if ($address) {
                        $address .= $Store->store_address;
                        $address = trim($address);
                        $api_res = $this->query_address($address, env('LBS_KEY'));
                        if ($api_res['status'] == '1') {
                            $store_lng = $api_res['result']['lng'];
                            $store_lat = $api_res['result']['lat'];
                            $Store->update([
                                'lng' => $store_lng,
                                'lat' => $store_lat,
                            ]);
                            $Store->save();
                        }
                    }
                }
            }

            if ($status == '1') {
                $status_desc = "审核成功";
            }else{
                $status_desc = "审核失败";
            }

            $Store->status = $status;
            $Store->status_desc = $status_desc;
            if ($user->level == 0) {
                $Store->admin_status = $status;
                $Store->admin_status_desc = $status_desc;
            }

            $result = $Store->save();
            if ($result) {
                $config_id = $Store->config_id;
                $phone = $Store->people_phone;
                //审核通过后，发送短信息通知
                $config = SmsConfig::where('type', '8')->where('config_id', $config_id)->first();
                if ($config && $config->app_key && $phone) {
                    $phone = trim($phone);
//                    $salt_phone = substr($phone, 0, 3).'****'.substr($phone, 7); //手机号加盐
                    $sms_data = [
                        'Manage' => $phone, //商户管理账号
                        'password' => '000000'
                    ];
                    $sms_result = $this->sendSms($phone, $config->app_key, $config->app_secret, $config->SignName, $config->TemplateCode, $sms_data);
                    if ($sms_result->Code == 'OK') {
//                        Log::info('门店审核通过，'.$phone.'发送短信成功');
                    } else {
//                        Log::info('门店审核通过，'.$phone.'发送短信失败: '.$sms_result->Message);
                    }
                }

                return json_encode([
                    'status' => 1,
                    'message' => '门店状态更改成功',
                    'data' => $request->except(['token'])
                ]);
            } else {
                return json_encode([
                    'status' => 2,
                    'message' => '门店状态更改失败',
                    'data' => $request->except(['token'])
                ]);
            }

        } catch (\Exception $exception) {
            return json_encode([
                'status' => -1,
                'message' => $exception->getMessage() . $exception->getFile() .$exception->getLine()
            ]);
        }
    }

    public function rata_store(Request $request){
        try {
            $user = $this->parseToken();
            $store_id = $request->get('store_id', '');
            $storerata = $request->get('storerata', '');
            $basicrata = $request->get('basicrata', '');
            $withuser = $request->get('withuser', '');
            if($user->user_id!=1){
                return json_encode([
                    'status' => 2,
                    'message' => '你没有权限设置技术费率',
                    'data' => $request->except(['token'])
                ]);
            }
            $Store = Store::where('store_id', $store_id)->first();
            $stores = [
                'rata_store' => $Store->rata_store,
                'basic_rata' => $Store->basic_rata,
                'with_user' => $Store->with_user,
            ];
            if (!$Store) {
                return json_encode([
                    'status' => 2,
                    'message' => '门店不存在'
                ]);
            }
            // echo '<pre>';
            // print_r($_SERVER['REMOTE_ADDR']);
            // die;

            $Store->rata_store = $storerata;
            $Store->basic_rata = $basicrata;
            $Store->with_user = $withuser;
            $Store->rata_status = 1;
            $result = $Store->save();
            if ($result) {

                $this->operation_log($user->user_id, 2, '门店技术费率: 门店id: ' . $store_id . '由技术费率: ' . $stores['rata_store'] . ', 基本费率: ' . $stores['basic_rata'] . ', 代扣代理: ' . $stores['with_user'] . '修改为技术费率: ' . $Store->rata_store . ', 基本费率: ' . $Store->basic_rata . ', 代扣代理: ' . $Store->with_user); // 操作日志

                return json_encode([
                    'status' => 1,
                    'message' => '设置技术费率成功',
                    'data' => $request->except(['token'])
                ]);
            } else {
                return json_encode([
                    'status' => 2,
                    'message' => '设置技术费率失败',
                    'data' => $request->except(['token'])
                ]);
            }

        } catch (\Exception $exception) {
            return json_encode([
                'status' => -1,
                'message' => $exception->getMessage() . $exception->getFile() .$exception->getLine()
            ]);
        }
    }


    //删除门店
    public function del_store(Request $request)
    {
        try {
            $token = $this->parseToken();
            $store_id = $request->get('store_id', '');
            $branch_store_type = $request->get('type', '');  //伪删除分店传2
            $store_ids = explode(',', $store_id);
            $user = User::where('id', $token->user_id)->first();
            $hasPermission = $user->hasPermissionTo('删除门店');
            if (!$hasPermission) {
                return json_encode(['status' => 2, 'message' => '没有权限']);
            }
            foreach($store_ids as $k => $v){
                $Store = Store::where('store_id', $v)->first();
                if (!$Store) {
                    return json_encode(['status' => 2, 'message' => '门店不存在']);
                }
                //有分店不允许删除
                $sub_Store = Store::where('pid', $Store->id)->first();
                if ($sub_Store) {
                    return json_encode(['status' => 2, 'message' => $Store->store_name."有分店不允许删除请先删除分店"]);
                }

            }
            foreach ($store_ids as $k => $v) {
                //门店不存在
                $Store = Store::where('store_id', $v)->first();
                if (!$Store) {
                    continue;
                }

                //有分店不允许删除
                $sub_Store = Store::where('id', $Store->pid)->first();
                if ($sub_Store && empty($branch_store_type)) {
                    continue;
                }
                if($sub_Store){
                    $merchant_id = $sub_Store->merchant_id;
                }else{
                    $merchant_id = '0';
                }

                //不是自己或者不是下级的商户
                $users = $this->getSubIds($token->user_id); //获取所有下级
                if (!in_array($Store->user_id, $users)) {
                    continue;
                }

                if ($token->level == 0) {
                    $order = Order::where('store_id', $v)
                        ->select('id')
                        ->first();
                    if (!$order) {
                        //彻底删除门店
                        try {
                            DB::beginTransaction(); //开启事务

//                             $merchant_store_obj = MerchantStore::where('store_id', $v)->get();
// //                            Log::info($merchant_store_obj);
//                             if ($merchant_store_obj) {
//                                 $merchant_store_arr = $merchant_store_obj->toArray();
// //                                Log::info($merchant_store_arr);
//                                 if (is_array($merchant_store_arr) && $merchant_store_arr) {
//                                     foreach ($merchant_store_arr as $val) {
//                                         if ($val['merchant_id']!=$merchant_id) {
//                                             Merchant::find($val['merchant_id'])->delete();
//                                         }
// //                                        Merchant::find($val['merchant_id'])->delete();
//                                     }
//                                 }
//                             }
                            Store::where('store_id', $v)->delete();
                            StorePayWay::where('store_id', $v)->delete();
                            MerchantStore::where('store_id', $v)->delete();
                            Device::where('store_id', $v)->delete();
                            StoreImg::where('store_id', $v)->delete();
                            StoreBank::where('store_id', $v)->delete();

                            DB::commit();
                        } catch (\Exception $ex) {
                            DB::rollBack();
                            continue;
                        }
                    } else {
                        Store::where('store_id', $v)->update([
                            'is_delete' => 1,
                        ]);
                    }
                } else {
                    Store::where('store_id', $v)->update([
                        'is_delete' => 1,
                    ]);
                }

            }
            return json_encode([
                'status' => 1,
                'message' => '门店删除成功',
                'data' => [
                    'store_id' => $store_id,
                ]
            ]);
        } catch (\Exception $exception) {
            return json_encode([
                'status' => -1,
                'message' => $exception->getMessage().' | '.$exception->getFile().' | '.$exception->getLine()
            ]);
        }
    }


    //恢复门店
    public function rec_store(Request $request)
    {
        try {
            $token = $this->parseToken();
            $store_id = $request->get('store_id', '');
            $store_ids = explode(',', $store_id);

            $user = User::where('id', $token->user_id)->first();
            $hasPermission = $user->hasPermissionTo('恢复门店');

            if (!$hasPermission) {
                return json_encode(['status' => 2, 'message' => '没有权限']);
            }

            foreach ($store_ids as $k => $v) {
                //门店不存在
                $Store = Store::where('store_id', $v)->first();
                if (!$Store) {
                    continue;
                }

                //有分店不允许删除
                $sub_Store = Store::where('id', $Store->pid)->first();
                if ($sub_Store) {
                    continue;
                }

                //不是自己或者不是下级的商户
                $users = $this->getSubIds($token->user_id); //获取所有下级
                if (!in_array($Store->user_id, $users)) {
                    continue;
                }

                Store::where('store_id', $v)->update([
                    'is_delete' => 0,
                ]);
            }

            return json_encode([
                'status' => 1,
                'message' => '门店恢复成功',
                'data' => [
                    'store_id' => $store_id,
                ]
            ]);
        } catch (\Exception $exception) {
            return json_encode([
                'status' => -1,
                'message' => $exception->getMessage() . $exception->getFile()
            ]);
        }

    }


    //直接清除门店
    public function clear_store(Request $request)
    {
        try {
            $token = $this->parseToken();
            $store_id = $request->get('store_id', '');
            $store_ids = explode(',', $store_id);


            $user = User::where('id', $token->user_id)->first();
            $hasPermission = $user->hasPermissionTo('清除门店');
            if (!$hasPermission) {
                return json_encode([
                    'status' => 2,
                    'message' => '没有权限'
                ]);
            }

            foreach ($store_ids as $k => $v) {
                //门店不存在
                $Store = Store::where('store_id', $v)
                    ->select('is_delete')
                    ->first();
                if (!$Store) {
                    continue;
                }

                //有分店不允许删除
                $sub_Store = Store::where('id', $Store->pid)
                    ->select('id')
                    ->first();
                if ($sub_Store) {
                    continue;
                }

                //有设备清除不了
                $Device = Device::where('store_id', $v)->select('id')->first();
                if ($Device) {
                    continue;
                }

                //彻底删除门店
                //开启事务
                try {
                    DB::beginTransaction();

                    Store::where('store_id', $v)->delete();
                    StorePayWay::where('store_id', $v)->delete();
                    MerchantStore::where('store_id', $v)->delete();
                    Device::where('store_id', $v)->delete();
                    StoreImg::where('store_id', $v)->delete();
                    StoreBank::where('store_id', $v)->delete();

                    DB::commit();
                } catch (\Exception $e) {
                    DB::rollBack();
                    continue;
                }

            }
            return json_encode([
                'status' => 1,
                'message' => '门店删除成功',
                'data' => [
                    'store_id' => $store_id,
                ]
            ]);
        } catch (\Exception $exception) {
            return json_encode([
                'status' => -1,
                'message' => $exception->getMessage() . $exception->getFile()
            ]);
        }

    }


    //门店关闭接口
    public function col_store(Request $request)
    {
        try {
            $user = $this->parseToken();
            $store_id = $request->get('store_id', '');
            $store_ids = explode(',', $store_id);

            foreach ($store_ids as $k => $v) {
                $Store = Store::where('store_id', $v)->first();
                //门店不存在
                if (!$Store) {
                    continue;
                }
                $Store->is_close = 1;
                $Store->save();

            }
            return json_encode([
                'status' => 1,
                'message' => '门店关闭成功',
                'data' => [
                    'store_id' => $store_id,
                ]
            ]);
        } catch (\Exception $exception) {
            return json_encode([
                'status' => -1,
                'message' => $exception->getMessage() . $exception->getFile()
            ]);
        }

    }


    //门店开启接口
    public function ope_store(Request $request)
    {
        try {
            $user = $this->parseToken();
            $store_id = $request->get('store_id', '');
            $store_ids = explode(',', $store_id);

            //目前只有平台可以删除
            if ($user->level > 0) {
                return json_encode([
                    'status' => 2,
                    'message' => '暂时不支持开启门店'
                ]);
            }

            foreach ($store_ids as $k => $v) {
                $Store = Store::where('store_id', $v)->first();
                //门店不存在
                if (!$Store) {
                    continue;
                }
                $Store->is_close = 0;
                $Store->save();

            }
            return json_encode([
                'status' => 1,
                'message' => '门店开启成功',
                'data' => [
                    'store_id' => $store_id,
                ]
            ]);
        } catch (\Exception $exception) {
            return json_encode([
                'status' => -1,
                'message' => $exception->getMessage() . $exception->getFile()
            ]);
        }

    }


    public function update_user(Request $request){
        try {
            $public = $this->parseToken();
            $store_id = $request->get('store_id', '');
            $user_id = $request->get('user_id', '');
//            print_r($public);die;

            $store_ids = explode(',', $store_id);
            $user = User::where('id', $user_id)->first(); // 要转出的人
//            print_r($store_ids);die;

            if (!$user) {
                return json_encode([
                    'status' => 2,
                    'message' => '用户不存在'
                ]);
            }

            $user_rates = DB::table('user_rates')->where([ 'user_id' => $user->id ])->select('company')->distinct()->get(); // 转入代理商 开通的通道
            $user_rates = json_decode($user_rates);
            $user_rates = array_column($user_rates, 'company');

            $server_rates = DB::table('server_rates')->where([ 'user_id' => $user->id ])->select('company')->distinct()->get(); // 转入代理商 开通的通道
            $server_rates = json_decode($server_rates);
            $server_rates = array_column($server_rates, 'company');
            // print_r($user_rates);die;

            $config_id = $user->config_id;
            foreach ($store_ids as $k => $v) {
                $Store = Store::where('store_id', $v)->select('user_id', 'config_id', 'id', 'store_id')->first();
//                print_r($v);die;

                if (!$Store) { // 门店不存在
                    return json_encode([
                        'status' => 2,
                        'message' => '门店不存在'
                    ]);
                }

                $store_pay_ways = DB::table('store_pay_ways')->where([ 'store_id' => $Store->store_id ])->select('company')->distinct()->get();
                $store_pay_ways = json_decode($store_pay_ways);
                $store_pay_ways = array_column($store_pay_ways, 'company');
//                print_r($store_pay_ways);die;
                foreach($store_pay_ways as $key => $val){
                    if( !in_array($val, $user_rates) ){
                        return json_encode([
                            'status' => 0,
                            'message' => '请检查成本费率配置',
                            'data' => [
                                'store_id' => $store_id,
                            ]
                        ]);
                    }
//                    print_r($val);
//                    print_r($server_rates);die;

                    // if( in_array($val, ['saobei', 'pingan', 'nongjin', 'ccb', 'wftpay']) ){
                    //     if( !in_array($val, $server_rates) ){
                    //         return json_encode([
                    //             'status' => 0,
                    //             'message' => '请检查成本费率',
                    //             'data' => [
                    //                 'store_id' => $store_id,
                    //             ]
                    //         ]);
                    //     }
                    // }
                }
//                print_r($public);die;


                if ($public->pid > 1 & $config_id != $Store->config_id) {
                    return json_encode([
                        'status' => 2,
                        'message' => '门店不支持转移到其他服务商下面-config_id'
                    ]);
                }

                try {
                    DB::beginTransaction(); //开启事务
                    Store::where('store_id', $v)->update(['user_id' => $user_id]); // 门店转移
                    Store::where('pid', $Store->id)->update(['user_id' => $user_id]); // 分店也转移
                    DB::commit();

                    $this->operation_log($public->user_id, 1, '门店转移: 门店id: ' . $store_id . '由代理商id: '. $Store->user_id . '转移到代理商id: ' . $user_id); // 操作日志

                } catch (\Exception $e) {
                    DB::rollBack();
                    continue;
                }
            }
            return json_encode([
                'status' => 1,
                'message' => '门店转移成功',
                'data' => [
                    'store_id' => $store_id,
                ]
            ]);
        } catch (\Exception $exception) {
            return json_encode([
                'status' => -1,
                'message' => $exception->getMessage() . $exception->getFile()
            ]);
        }
    }


    public function time($time)
    {
        try {

            //去除中文
            $time = preg_replace('/([\x80-\xff]*)/i', '', $time);

            $is_date = strtotime($time) ? strtotime($time) : false;

            if ($is_date) {
                $time = date('Y-m-d', strtotime($time));
            }

            return $time;
        } catch (\Exception $ex) {
            Log::info($ex->getMessage().' | '.$ex->getFile().' | '.$ex->getFile());
        }

        return $time;
    }


    //汇付-获取商户信息接口
    public function get_mer_id(Request $request)
    {
        try {
            $store_id = $request->get('store_id', '');
            $ways_type = $request->get('ways_type', '');
            $token = $this->parseToken();

            if (17999 < $ways_type && $ways_type < 18999) {
                $store_info = Store::where('store_id', $store_id)
                    ->where('is_close', '0')
                    ->where('is_delete', '0')
                    ->first();
                if (!$store_info) {
                    return json_encode([
                        'status' => '2',
                        'message' => '门店不存在或状态异常'
                    ]);
                }

                $store_pid = $store_info->pid ?? '';

                $hui_pay_config_obj = new \App\Api\Controllers\Config\HuiPayConfigController();
                $hui_pay_config = $hui_pay_config_obj->hui_pay_config($store_info->config_id);
                if (!$hui_pay_config) {
                    return json_encode([
                        'status' => '2',
                        'message' => '汇付配置不存在'
                    ]);
                }

                $hui_pay_store = $hui_pay_config_obj->hui_pay_merchant($store_id, $store_pid);
                if (!$hui_pay_store) {
                    return json_encode([
                        'status' => '2',
                        'message' => '汇付商户不存在'
                    ]);
                }

                $hui_pay_base_obj = new \App\Api\Controllers\HuiPay\BaseController();

                $user_cust_id = $hui_pay_store->user_cust_id;
                if (!$user_cust_id) {
                    return json_encode([
                        'status' => '2',
                        'message' => '汇付用户客户号不存在'
                    ]);
                }

                $mer_id = $hui_pay_store->mer_id;  //商户号
                if ($mer_id) {
                    $data = [
                        'mer_id' => $mer_id,
                        'device_id' => $hui_pay_store->device_id ?? ''
                    ];
                } else {  //商户号不存在就去查
                    $sign_data = ['user_cust_id' => $user_cust_id];
                    $post_data = [
                        'data'=> $sign_data,
                        'sign_type'=> 'RSA2',
                        'mer_cust_id'=> $hui_pay_config->mer_cust_id,
                        'source_num'=> $hui_pay_config->org_id
                    ];
                    $result = $hui_pay_base_obj->execute($hui_pay_base_obj->store_detail, $post_data, $sign_data, $hui_pay_config->private_key, $hui_pay_config->public_key);
//                    var_dump($result);die;
                    if ($result['status'] == '000000' && isset($result['data']) && !empty($result['data'])) {
                        $data_json = json_decode($result['data'], true);
                        $data = [
                            'mer_id'=>$data_json['mer_id']
                        ];

                        $hui_pay_store->update($data);
                    } else {
                        return json_encode([
                            'status' => '2',
                            'message' => $result['message']
                        ]);
                    }
                }

            } else {
                return json_encode([
                    'status' => '2',
                    'message' => '支付类型参数不对'
                ]);
            }

        } catch (\Exception $ex) {
            return json_encode([
                'status' => $ex->getCode(),
                'message' => $ex->getMessage()
            ]);
        }

        $this->status = '1';
        $this->message = '数据返回成功';
        return $this->format($data);
    }


    //汇付-门店绑定机具
    public function hui_pay_bind(Request $request)
    {
        try {
//            var_dump($request->all());die;
            $token = $this->parseToken();
            $store_id = $request->get('store_id', '');
            $ways_type = $request->get('ways_type', '');
            $device_id = $request->get('device_id', ''); //汇付机具id

            if (17999 < $ways_type && $ways_type < 18999) {
                $store_info = Store::where('store_id', $store_id)
                    ->where('is_close', '0')
                    ->where('is_delete', '0')
                    ->first();
                if (!$store_info) {
                    return json_encode([
                        'status' => '2',
                        'message' => '门店不存在或状态异常'
                    ]);
                }

//                $store_pid = $store_info->pid ?? '';

                $hui_pay_config_obj = new HuiPayConfigController();
                $hui_pay_config = $hui_pay_config_obj->hui_pay_config($store_info->config_id);
                if (!$hui_pay_config) {
                    return json_encode([
                        'status' => '2',
                        'message' => '汇付配置不存在'
                    ]);
                }

                $hui_pay_store = $hui_pay_config_obj->hui_pay_merchant($store_id, '');
                if (!$hui_pay_store) {
                    return json_encode([
                        'status' => '2',
                        'message' => '汇付商户不存在'
                    ]);
                }

                $hui_pay_base_obj = new \App\Api\Controllers\HuiPay\BaseController();

                $user_cust_id = $hui_pay_store->user_cust_id;
                if (!$user_cust_id) {
                    return json_encode([
                        'status' => '2',
                        'message' => '汇付用户客户号不存在'
                    ]);
                }

                $shop_id = $hui_pay_store->shop_id;  //绑定门店id
                if (!$shop_id) {
                    return json_encode([
                        'status' => '2',
                        'message' => '汇付绑定门店id不存在'
                    ]);
                }

                $huipay_device_id = $hui_pay_store->device_id;
                //门店是否绑定设备
                if ($huipay_device_id && ($huipay_device_id != $device_id)) {
                    //表中已存在机具id且和新id不同解绑
                    $sign_data = [
                        'device_id' => $device_id,  //必须，设备号
                        'shop_id' => $shop_id,	 //必须，绑定门店id
                    ];
                    $post_data = [
                        'data'=> $sign_data,
                        'sign_type'=> 'RSA2',
                        'mer_cust_id'=> $hui_pay_config->mer_cust_id,
                        'source_num'=> $hui_pay_config->org_id
                    ];
                    $result = $hui_pay_base_obj->execute($hui_pay_base_obj->device_unbind, $post_data, $sign_data, $hui_pay_config->private_key, $hui_pay_config->public_key);
                    if ($result['status'] == '0') {
                        $this->status = '0';
                        $this->message = '解绑验签失败';
                        return $this->format();
                    }
//                    var_dump($result);die;
                    if ($result['status'] == '000000') {
                        //解绑成功后，有新的机具id就绑定
                        if ($device_id) {
                            $bind_sign_data = [
                                'device_id' => $device_id,  //必须，设备号
                                'device_name' => '汇付机具',	 //必须，设备名称
                                'shop_id' => $shop_id,	 //必须，绑定门店id
                            ];
                            $bind_post_data = [
                                'data'=> $bind_sign_data,
                                'sign_type'=> 'RSA2',
                                'mer_cust_id'=> $hui_pay_config->mer_cust_id,
                                'source_num'=> $hui_pay_config->org_id
                            ];
                            $bind_result = $hui_pay_base_obj->execute($hui_pay_base_obj->device_bind, $bind_post_data, $bind_sign_data, $hui_pay_config->private_key, $hui_pay_config->public_key);
//                    var_dump($bind_result);die;
                            if ($bind_result['status'] == '0') {
                                return json_encode([
                                    'status' => '0',
                                    'message' => '更改机具绑定验签失败'
                                ]);
                            }
                            if ($bind_result['status'] == '000000') {
                                $this->status = '1';
                                $this->message = '汇付机具重新绑定成功';
                                return $this->format();
                            } else {
                                return json_encode([
                                    'status' => '2',
                                    'message' => $bind_result['message']
                                ]);
                            }
                        }

                    } else {
                        //解绑失败
                        return json_encode([
                            'status' => '2',
                            'message' => $result['message']
                        ]);
                    }
                } else {
                    //表中没有机具id，绑定新的机具id
                    if ($device_id) {
                        $bind_sign_data = [
                            'device_id' => $device_id,  //必须，设备号
                            'device_name' => '汇付机具',	 //必须，设备名称
                            'shop_id' => $shop_id,	 //必须，绑定门店id
                        ];
                        $bind_post_data = [
                            'data'=> $bind_sign_data,
                            'sign_type'=> 'RSA2',
                            'mer_cust_id'=> $hui_pay_config->mer_cust_id,
                            'source_num'=> $hui_pay_config->org_id
                        ];
                        $bind_result = $hui_pay_base_obj->execute($hui_pay_base_obj->device_bind, $bind_post_data, $bind_sign_data, $hui_pay_config->private_key, $hui_pay_config->public_key);
//                    var_dump($bind_result);die;
                        if ($bind_result['status'] == '0') {
                            return json_encode([
                                'stauts' => '0',
                                'message' => '绑定机具验签失败'
                            ]);
                        }
                        if ($bind_result['status'] == '000000') {
                            $this->status = '1';
                            $this->message = '汇付机具绑定成功';
                            return $this->format();
                        } else {
                            return json_encode([
                                'status' => '2',
                                'message' => $bind_result['message']
                            ]);
                        }
                    }
                }

            } else {
                return json_encode([
                    'status' => '2',
                    'message' => '通道类型参数不对'
                ]);
            }

        } catch (\Exception $ex) {
            return json_encode([
                'status' => $ex->getCode(),
                'message' => $ex->getMessage()
            ]);
        }

    }


    public function sendSms($phone, $app_key, $app_secret, $SignName, $TemplateCode, $data)
    {
        $demo = new AliSms($app_key, $app_secret);
        $response = $demo->sendSms(
            $SignName, // 短信签名
            $TemplateCode, // 短信模板编号
            $phone, // 短信接收者
            $data
        );

        return $response;
    }


    /**
     * 通过地址解析获得经纬度
     * @param string $key_words  地址（注：地址中请包含城市名称，否则会影响解析效果）
     * @param string $key  开发密钥（Key）
     * @return array
     */
    public function query_address($key_words, $key)
    {
        $url = "https://apis.map.qq.com/ws/geocoder/v1/?key=". $key ."&address=". $key_words;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $output = curl_exec($ch);

        $errmsg = '';
        if (curl_errno($ch)) {
            $errmsg = '腾讯地图api请求错误：'.curl_error($ch);
        }

        curl_close($ch); //释放curl句柄

        if ($errmsg != '') {
//            Log::info($errmsg);
        }

        $res = json_decode($output, true);
        if ($res['status'] == '0') {
            $data = [
                'status' => '1',
                'result' => $res['result']['location']
            ];
        } else {
            $data = [
                'status' => '0',
                'message' => $res['message']
            ];
        }

        return $data;
    }


    //哆啦宝-获取相关信息
    public function dlb_store_info(Request $request)
    {
        try {
            $token = $this->parseToken();
            $store_id = $request->get('store_id', ''); //门店id
            $select_type = $request->get('select_type', ''); //查询数据类型
            $industry_num = $request->get('industry_num', ''); //店铺一级行业code
            $province_code = $request->get('province_code', ''); //省份code
            $bank_keywords = $request->get('bank_keywords', ''); //银行关键字
            $bank_code = $request->get('bank_code', ''); //银行code
            $sub_branch_keywords = $request->get('sub_branch_keywords', ''); //支行关键字

            $store_info = Store::where('store_id', $store_id)
                ->where('is_close', '0')
                ->where('is_delete', '0')
                ->first();
            if (!$store_info) {
                return json_encode([
                    'status' => '2',
                    'message' => '门店不存在或状态异常'
                ]);
            }

            $config_id = $store_info->config_id;
            $timestamp = time();

            $manager = new \App\Api\Controllers\DuoLaBao\ManageController();
            $dlb_config = $manager->pay_config($config_id);
            if (!$dlb_config) {
                return json_encode([
                    'status' => 2,
                    'message' => '哆啦宝配置配置不存在请检查配置'
                ]);
            }

            //店铺一级行业
            if ($select_type == '1') {
                $dlb_primary_industry = Cache::get('primary_industry');
                if (!$dlb_primary_industry) {
                    $dlb_token = $manager->dlb_sign($timestamp, $manager->dlb_category, '', $dlb_config->secret_key);
                    $dlb_header = array(
                        "Content-Type" => "application/json",
                        "accessKey" => $dlb_config->access_key,
                        "timestamp" => $timestamp,
                        "token" => $dlb_token,
                    );
                    $dlb_result = $manager->post_func($manager->dlb_host_url . $manager->dlb_category, '', $dlb_header, '10', '1');
                    if ($dlb_result['status'] == '1') {
                        $result = json_decode($dlb_result['data'], true);
                        if ($result['result'] == 'error') {
                            return json_encode([
                                'status' => 2,
                                'message' => $result['error']['errorCode']
                            ]);
                        }

                        Cache::put('primary_industry', $result['data']['industryList'], 1000000);

                        return json_encode([
                            'status' => 1,
                            'data' => $result['data']['industryList'],
                            'message' => '获取成功'
                        ]);
                    } else {
                        return json_encode([
                            'status' => 0,
                            'data' => [],
                            'message' => $dlb_result['message']
                        ]);
                    }
                }

                return json_encode([
                    'status' => 1,
                    'data' => $dlb_primary_industry,
                    'message' => '获取成功'
                ]);
            }

            //店铺二级行业
            if ($select_type == '2' && $industry_num) {
                $dlb_second_industry = Cache::get('second_industry_' . $industry_num);
                if (!$dlb_second_industry) {
                    $dlb_token = $manager->dlb_sign($timestamp, $manager->dlb_second_category . $industry_num, '', $dlb_config->secret_key);
                    $dlb_header = array(
                        "Content-Type" => "application/json",
                        "accessKey" => $dlb_config->access_key,
                        "timestamp" => $timestamp,
                        "token" => $dlb_token,
                    );
                    $dlb_result = $manager->post_func($manager->dlb_host_url . $manager->dlb_second_category . $industry_num, '', $dlb_header, '10', '1');
                    if ($dlb_result['status'] == '1') {
                        $result = json_decode($dlb_result['data'], true);
                        if ($result['result'] == 'error') {
                            return json_encode([
                                'status' => 2,
                                'message' => $result['error']['errorCode']
                            ]);
                        }

                        Cache::put('second_industry_' . $industry_num, $result['data']['industryList'], 1000000);

                        return json_encode([
                            'status' => 1,
                            'data' => $result['data']['industryList'],
                            'message' => '获取成功'
                        ]);
                    } else {
                        return json_encode([
                            'status' => 0,
                            'data' => [],
                            'message' => $dlb_result['message']
                        ]);
                    }
                }

                return json_encode([
                    'status' => 1,
                    'data' => $dlb_second_industry,
                    'message' => '获取成功'
                ]);
            }

            //省份
            if ($select_type == '3') {
                $dlb_province = Cache::get('dlb_province');
                if (!$dlb_province) {
                    //哆啦宝省份数据
                    $dlb_token = $manager->dlb_sign($timestamp, $manager->dlb_province, '', $dlb_config->secret_key);
                    $dlb_header = array(
                        "Content-Type" => "application/json",
                        "accessKey" => $dlb_config->access_key,
                        "timestamp" => $timestamp,
                        "token" => $dlb_token,
                    );
                    $dlb_result = $manager->post_func($manager->dlb_host_url . $manager->dlb_province, '', $dlb_header, '10', '1');
                    if ($dlb_result['status'] == '1') {
                        $result = json_decode($dlb_result['data'], true);
                        if ($result['result'] == 'error') {
                            return json_encode([
                                'status' => 2,
                                'message' => $result['error']['errorCode']
                            ]);
                        }

                        Cache::put('dlb_province', $result['data']['addressList'], 1000000);

                        return json_encode([
                            'status' => 1,
                            'message' => '数据请求成功',
                            'data' => $result['data']['addressList']
                        ]);
                    } else {
                        return json_encode([
                            'status' => 0,
                            'message' => $dlb_result['message']
                        ]);
                    }
                }

                return json_encode([
                    'status' => 1,
                    'message' => '数据请求成功',
                    'data' => $dlb_province
                ]);

            }

            //城市
            if($select_type == '4' && $province_code) {
                $dlb_city = Cache::get('dlb_city_' . $province_code);
                if (!$dlb_city) {
                    //哆啦宝城市数据
                    $dlb_token = $manager->dlb_sign($timestamp, $manager->dlb_city . $province_code, '', $dlb_config->secret_key);
                    $dlb_header = array(
                        "Content-Type" => "application/json",
                        "accessKey" => $dlb_config->access_key,
                        "timestamp" => $timestamp,
                        "token" => $dlb_token,
                    );
                    $dlb_result = $manager->post_func($manager->dlb_host_url . $manager->dlb_city . $province_code, '', $dlb_header, '10', '1');
    //                var_dump($dlb_result);die;
                    if ($dlb_result['status'] == '1') {
                        $result = json_decode($dlb_result['data'], true);
                        if ($result['result'] == 'error') {
                            return json_encode([
                                'status' => 2,
                                'message' => $result['error']['errorCode']
                            ]);
                        }

                        Cache::put('dlb_city_' . $province_code, $result['data']['addressList'], 1000000);

                        return json_encode([
                            'status' => 1,
                            'data' => $result['data']['addressList']
                        ]);
                    } else {
                        return json_encode([
                            'status' => 0,
                            'message' => $dlb_result['message']
                        ]);
                    }
                }

                return json_encode([
                    'status' => 1,
                    'data' => $dlb_city
                ]);
            }

            //银行名称
            if($select_type == '5' && $bank_keywords) {
                $dlb_bank_info = Cache::get('dlb_bank_info' . $bank_keywords);
                if (!$dlb_bank_info) {
                    //哆啦宝银行名称
                    $new_bank_keywords = urlencode($bank_keywords);
                    $dlb_token = $manager->dlb_sign($timestamp, $manager->dlb_bank_list . $bank_keywords, '', $dlb_config->secret_key);
                    $dlb_header = array(
                        "Content-Type" => "application/json",
                        "accessKey" => $dlb_config->access_key,
                        "timestamp" => $timestamp,
                        "token" => $dlb_token,
                    );
                    $dlb_result = $manager->post_func($manager->dlb_host_url . $manager->dlb_bank_list . $new_bank_keywords, '', $dlb_header, '10', '1');
//                    var_dump($dlb_result);die;
                    if ($dlb_result['status'] == '1') {
                        $result = json_decode($dlb_result['data'], true);
                        if ($result['result'] == 'error') {
                            return json_encode([
                                'status' => 2,
                                'message' => $result['error']['errorCode']
                            ]);
                        }

                        Cache::put('dlb_bank_info' . $bank_keywords, $result['data']['bankList'], 1000000);

                        return json_encode([
                            'status' => 1,
                            'data' => $result['data']['bankList']
                        ]);
                    } else {
                        return json_encode([
                            'status' => 0,
                            'message' => $dlb_result['message']
                        ]);
                    }
                }

                return json_encode([
                    'status' => 1,
                    'data' => $dlb_bank_info
                ]);
            }

            //银行分行名称
            if($select_type == '6' && $bank_code && $sub_branch_keywords) {
                $dlb_sub_branch_bank = Cache::get('dlb_sub_branch_bank' . $bank_code . $sub_branch_keywords);
                if (!$dlb_sub_branch_bank) {
                    //哆啦宝-银行分行名称
                    $new_sub_branch_keywords = urlencode($sub_branch_keywords);
                    $dlb_token = $manager->dlb_sign($timestamp, $manager->dlb_sub_bank_list . $bank_code .'/'. $sub_branch_keywords, '', $dlb_config->secret_key);
                    $dlb_header = array(
                        "Content-Type" => "application/json",
                        "accessKey" => $dlb_config->access_key,
                        "timestamp" => $timestamp,
                        "token" => $dlb_token,
                    );
                    $dlb_result = $manager->post_func($manager->dlb_host_url . $manager->dlb_sub_bank_list . $bank_code .'/'. $new_sub_branch_keywords, '', $dlb_header, '10', '1');
//                    var_dump($dlb_result);die;
                    if ($dlb_result['status'] == '1') {
                        $result = json_decode($dlb_result['data'], true);
                        if ($result['result'] == 'error') {
                            return json_encode([
                                'status' => 2,
                                'message' => $result['error']['errorCode']
                            ]);
                        }

                        Cache::put('dlb_sub_branch_bank' . $bank_code . $sub_branch_keywords, $result['data']['bankSubList'], 1000000);

                        return json_encode([
                            'status' => 1,
                            'data' => $result['data']['bankSubList']
                        ]);
                    } else {
                        return json_encode([
                            'status' => 0,
                            'message' => $dlb_result['message']
                        ]);
                    }
                }

                return json_encode([
                    'status' => 1,
                    'data' => $dlb_sub_branch_bank
                ]);
            }

            //支付银行
            if($select_type == '7') {
                $dlb_pay_bank_info = Cache::get('dlb_pay_bankinfo');
                if (!$dlb_pay_bank_info) {
                    //哆啦宝-支付银行
                    $dlb_token = $manager->dlb_sign($timestamp, $manager->dlb_pay_bankinfo, '', $dlb_config->secret_key);
                    $dlb_header = array(
                        "Content-Type" => "application/json",
                        "accessKey" => $dlb_config->access_key,
                        "timestamp" => $timestamp,
                        "token" => $dlb_token,
                    );
                    $dlb_result = $manager->post_func($manager->dlb_host_url . $manager->dlb_pay_bankinfo, '', $dlb_header, '10', '1');
//                    var_dump($dlb_result);die;
                    if ($dlb_result['status'] == '1') {
                        $result = json_decode($dlb_result['data'], true);
                        if ($result['result'] == 'error') {
                            return json_encode([
                                'status' => 2,
                                'message' => $result['error']['errorCode']
                            ]);
                        }

                        Cache::put('dlb_pay_bankinfo', $result['data']['payBankList'], 1000000);

                        return json_encode([
                            'status' => 1,
                            'data' => $result['data']['payBankList']
                        ]);
                    } else {
                        return json_encode([
                            'status' => 0,
                            'message' => $dlb_result['message']
                        ]);
                    }
                }

                return json_encode([
                    'status' => 1,
                    'data' => $dlb_pay_bank_info
                ]);
            }

        } catch (\Exception $ex) {
            return json_encode([
                'status' => '-1',
                'message' => $ex->getMessage() .' | '. $ex->getFile().' | '.$ex->getLine()
            ]);
        }
    }


    /**
     * 判断是否为合法的身份证号码
     * @param string $vStr 身份证号码
     * @return int
     */
    protected function check_sfz($vStr)
    {
        $vCity = ['11','12','13','14','15','21','22', '23','31','32','33','34','35','36', '37','41','42','43','44','45','46', '50','51','52','53','54','61','62', '63','64','65','71','81','82','91'];

        if (!preg_match('/^([\d]{17}[xX\d]|[\d]{15})$/', $vStr)) return false;

        if (!in_array(substr($vStr, 0, 2), $vCity)) return false;

        $vStr = preg_replace('/[xX]$/i', 'a', $vStr);
        $vLength = strlen($vStr);
        if ($vLength == 18) {
            $vBirthday = substr($vStr, 6, 4) . '-' . substr($vStr, 10, 2) . '-' . substr($vStr, 12, 2);
        } else {
            $vBirthday = '19' . substr($vStr, 6, 2) . '-' . substr($vStr, 8, 2) . '-' . substr($vStr, 10, 2);
        }

        if (date('Y-m-d', strtotime($vBirthday)) != $vBirthday) return false;
        if ($vLength == 18) {
            $vSum = 0;
            for ($i = 17 ; $i >= 0 ; $i--) {
                $vSubStr = substr($vStr, 17 - $i, 1);
                $vSum += (pow(2, $i) % 11) * (($vSubStr == 'a') ? 10 : intval($vSubStr , 11));
            }
            if($vSum % 11 != 1) return false;
        }

        return true;
    }


    //验证进件参数
    public function checkAddStoreInfo(Request $request)
    {
        try{
            $token = $this->parseToken();
            $people_phone = $request->get('people_phone', '');
            $store_id = $request->get('store_id', '');

            //用户是否存在
            $user_id = $token->user_id;
            $user_info = User::where('id', $user_id)
                ->where('is_delete', '0')
                ->first();
            if (!$user_info) {
                return json_encode([
                    'status' => '2',
                    'message' => '用户不存在或被禁用，请联系管理员'
                ]);
            }

            //门店是否存在
            $store_info = Store::where('store_id', $store_id)
                ->select('id')
                ->first();
            if ($store_info) {
                $is_store = 1;
            } else {
                $is_store = 0;
            }

            //判断手机号是否被注册
            if ($people_phone) {
                //验证手机号
                if (!preg_match("/^1[3456789]{1}\d{9}$/", $people_phone)) {
                    return json_encode([
                        'status' => '2',
                        'message' => '手机号码格式不正确'
                    ]);
                }

                $is_merchant = 0;
                $Merchant = Merchant::where('phone', $people_phone)
                    ->select('id')
                    ->first();
                if ($Merchant) {
                    $is_merchant = 1;
                }

                if ($is_merchant == 1 && $is_store == 0) {
                    return json_encode([
                        'status' => '2',
                        'message' => '手机号已被商户注册,请更换'
                    ]);
                }

                return json_encode([
                    'status' => '1',
                    'message' => '手机号尚未被注册'
                ]);
            } else {
                return json_encode([
                    'status' => '2',
                    'message' => '手机号不能为空'
                ]);
            }

        } catch (\Exception $ex) {
            return json_encode([
                'status' => '-1',
                'message' => $ex->getMessage() .'|'. $ex->getFile() .'|'. $ex->getLine()
            ]);
        }
    }


    //海科融通 经营范围
    public function business_scope(Request $request)
    {
        try {
            $token = $this->parseToken();
            $store_id = $request->get('store_id', '');

            $hkrt_data = Cache::get('hkrt_business_scope');
            if (!$hkrt_data) {
                $hkrt_data = HkrtBusinessScope::select('code', 'name')->get();
                Cache::put('hkrt_business_scope', $hkrt_data, 1000000);
            }

            if ($hkrt_data) {
                $this->status = '1';
                $this->message = '返回数据成功';
                $this->data = $hkrt_data;
                return $this->format($hkrt_data);
            } else {
                return json_encode([
                    'status' => '0',
                    'message' => '',
                    'data' => []
                ]);
            }

        } catch (\Exception $ex) {
            return json_encode([
                'status' => '-1',
                'message' => $ex->getMessage() . '|' . $ex->getFile() . '|' . $ex->getLine()
            ]);
        }
    }


    //编辑门店信息 TODO:尚未完成
    public function update_store(Request $request)
    {
        try {
            $token = $this->parseToken();
            $user_id = $token->user_id;
            $data = $request->except(['token']);

            $hkrt_bus_scope_code = $request->post('hkrt_bus_scope_code', ''); //海科融通经营范围
            $reserved_mobile = $request->post('reserved_mobile', ''); //预留手机号

            $store_id = $request->get('store_id', '');
            $config_id = $token->config_id;

            $user_id = $request->get('user_id', $user_id);

            //法人信息
            $people_phone = $request->get('people_phone', '');
            $store_email = $request->get('store_email', '');
            $head_name = $request->get('head_name', '');
            $head_sfz_no = $request->get('head_sfz_no', '');
            $bank_sfz_no = $request->get('bank_sfz_no', '');
            $people = $request->get('people', '');

            $head_sfz_img_a = $request->get('head_sfz_img_a', '');
            $head_sfz_img_b = $request->get('head_sfz_img_b', '');

            $head_sfz_time = $request->get('head_sfz_time', '');
            $head_sfz_stime = $request->get('head_sfz_stime', '');
            $head_sfz_time = $this->time($head_sfz_time);
            $head_sfz_stime = $this->time($head_sfz_stime);

            //门店信息
            $store_name = $request->get('store_name', '');
            $store_short_name = $request->get('store_short_name', $store_name);
            $province_code = $request->get('province_code', '');
            $city_code = $request->get('city_code', '');
            $area_code = $request->get('area_code', '');
            $store_address = $request->get('store_address', '');
            $store_type = $request->get('store_type', '');
            $store_type_name = $request->get('store_type_name', '');
            $dlb_micro_biz_type = $request->get('DLB_micro_biz_type', '');
            $dlb_industry = $request->get('DLB_industry', '');
            $dlb_second_industry = $request->get('DLB_second_industry', '');
            $dlb_province = $request->get('DLB_province', '');
            $dlb_city = $request->get('DLB_city', '');
            $dlb_bank = $request->get('DLB_bank', '');
            $dlb_sub_bank = $request->get('DLB_sub_bank', '');
            $dlb_pay_bank_list = $request->get('DLB_pay_bank_list', '');
            if ($dlb_pay_bank_list) {
                $dlb_pay_bank_list = json_encode($dlb_pay_bank_list);
            }

            $category_id = $request->get('category_id', '');
            $category_name = $request->get('category_name', '');
            $store_logo_img = $request->get('store_logo_img', '');
            $store_img_a = $request->get('store_img_a', '');
            $store_img_a_people = $request->get('store_img_a_people', '');
            $store_img_b = $request->get('store_img_b', '');
            $store_img_c = $request->get('store_img_c', '');
            $weixin_name = $request->get('weixin_name', '');
            $weixin_no = $request->get('weixin_no', '');
            $alipay_name = $request->get('alipay_name', '');
            $alipay_account = $request->get('alipay_account', '');

            //收款信息
            $store_alipay_account = $request->get('store_alipay_account', '');
            $store_bank_no = $request->get('store_bank_no', '');
            $store_bank_phone = $request->get('store_bank_phone', '');
            $store_bank_name = $request->get('store_bank_name', '');
            $store_bank_type = $request->get('store_bank_type', '');
            $bank_name = $request->get('bank_name', '');
            $bank_no = $request->get('bank_no', '');
            $sub_bank_name = $request->get('sub_bank_name', '');
            $bank_province_code = $request->get('bank_province_code', '');
            $bank_city_code = $request->get('bank_city_code', '');
            $bank_area_code = $request->get('bank_area_code', '');
            $bank_img_a = $request->get('bank_img_a', '');
            $bank_img_b = $request->get('bank_img_b', '');

            $bank_sfz_img_a = $request->get('bank_sfz_img_a', '');
            $bank_sfz_img_b = $request->get('bank_sfz_img_b', '');
            $bank_sc_img = $request->get('bank_sc_img', '');
            $store_auth_bank_img = $request->get('store_auth_bank_img', '');

            $bank_sfz_time = $request->get('bank_sfz_time', '');
            $bank_sfz_stime = $request->get('bank_sfz_stime', '');
            $bank_sfz_time = $this->time($bank_sfz_time);
            $bank_sfz_stime = $this->time($bank_sfz_stime);

            //证照信息
            $store_license_no = $request->get('store_license_no', '');
            $store_license_time = $request->get('store_license_time', '');
            $store_license_stime = $request->get('store_license_stime', '');

            $head_sc_img = $request->get('head_sc_img', '');
            $head_store_img = $request->get('head_store_img', '');

            $store_license_img = $request->get('store_license_img', '');
            $store_industrylicense_img = $request->get('store_industrylicense_img', '');
            $store_other_img_a = $request->get('store_other_img_a', '');
            $store_other_img_b = $request->get('store_other_img_b', '');
            $store_other_img_c = $request->get('store_other_img_c', '');
            $dlb_settler_hold_settlecard = $request->get('DLB_settler_hold_settlecard', ''); //哆啦宝-结算人手持结算卡

            //必填字段
            $check_data = [
                'store_id' => '门店ID',
                'store_type' => '商户类型',
                'store_type_name' => '商户类型名称',
            ];
            $check = $this->check_required($request->except(['token']), $check_data);
            if ($check) {
                return json_encode([
                    'status' => '2',
                    'message' => $check
                ]);
            }

            //拼装门店信息
            $stores = [
                'config_id' => $config_id,
                'user_id' => $user_id,
                'merchant_id' => '',
                'store_id' => $store_id,
                'store_name' => $store_name,
//                'store_type' => $store_type,
                'store_type_name' => $store_type_name,
                'store_email' => $store_email,
                'store_short_name' => $store_short_name,
                'people' => $people, //负责人
                'people_phone' => $people_phone,
                'province_code' => $province_code,
                'city_code' => $city_code,
                'area_code' => $area_code,
                'store_address' => $store_address,
                'head_name' => $head_name, //法人
                'head_sfz_no' => $head_sfz_no,
                'head_sfz_time' => $head_sfz_time,
                'head_sfz_stime' => $head_sfz_stime,
                'category_id' => $category_id,
                'category_name' => $category_name,
                'store_license_no' => $store_license_no,
                'store_license_time' => $store_license_time,
                'store_license_stime' => $store_license_stime,
                'weixin_name' => isset($weixin_name) ? $weixin_name : "",
                'weixin_no' => isset($weixin_no) ? $weixin_no : "",
                'alipay_name' => isset($alipay_name) ? $alipay_name : "",
                'alipay_account' => isset($alipay_account) ? $alipay_account : "",
                'dlb_micro_biz_type' => $dlb_micro_biz_type, //哆啦宝-经营类型
                'dlb_industry' => $dlb_industry, //哆啦宝店铺一级行业
                'dlb_second_industry' => $dlb_second_industry, //店铺二级行业
                'dlb_province' => $dlb_province, //哆啦宝-商户省份
                'dlb_city' => $dlb_city, //哆啦宝商户城市
                'dlb_bank' => $dlb_bank, //哆啦宝-银行名称
                'dlb_sub_bank' => $dlb_sub_bank, //哆啦宝-银行分行名称
                'dlb_pay_bank_list' => $dlb_pay_bank_list, //支付类型+费率
            ];

            if ($store_type) {
                $stores['store_type'] = $store_type;
            }
            if ($province_code) {
                $stores['province_name'] = $this->city_name($province_code);
            }
            if ($city_code) {
                $stores['city_name'] = $this->city_name($city_code);
            }
            if ($area_code) {
                $stores['area_name'] = $this->city_name($area_code);
            }
            if ($hkrt_bus_scope_code) {
                $stores['hkrt_bus_scope_code'] = $hkrt_bus_scope_code; //海科融通经营范围
            }

            //图片信息
            $store_imgs = [
                'store_id' => $store_id,
                'head_sfz_img_a' => $head_sfz_img_a,
                'head_sfz_img_b' => $head_sfz_img_b,
                'store_license_img' => $store_license_img,
                'store_industrylicense_img' => $store_industrylicense_img,
                'store_logo_img' => $store_logo_img,
                'store_img_a' => $store_img_a,
                "store_img_a_people"=>$store_img_a_people,
                'store_img_b' => $store_img_b,
                'store_img_c' => $store_img_c,
                'bank_img_a' => $bank_img_a,
                'bank_img_b' => $bank_img_b,
                'store_other_img_a' => $store_other_img_a,
                'store_other_img_b' => $store_other_img_b,
                'store_other_img_c' => $store_other_img_c,
                'head_sc_img' => $head_sc_img,
                'head_store_img' => $head_store_img,
                'bank_sfz_img_a' => $bank_sfz_img_a,
                'bank_sfz_img_b' => $bank_sfz_img_b,
                'bank_sc_img' => $bank_sc_img,
                'store_auth_bank_img' => $store_auth_bank_img,
                'dlb_settler_hold_settlecard' => $dlb_settler_hold_settlecard, //哆啦宝-结算人手持结算卡
            ];

            //银行卡信息
            $store_banks = [
                'store_id' => $store_id,
                'store_bank_no' => $store_bank_no,
                'store_bank_name' => $store_bank_name,
                'store_bank_phone' => $store_bank_phone,
                'store_bank_type' => $store_bank_type,
                'bank_name' => $bank_name,
                'bank_no' => $bank_no,
                'sub_bank_name' => $sub_bank_name,
                'bank_province_code' => $bank_province_code,
                'bank_city_code' => $bank_city_code,
                'bank_area_code' => $bank_area_code,
                'bank_sfz_no' => $bank_sfz_no,//持卡人默认是法人
                'bank_sfz_time' => $bank_sfz_time,
                'bank_sfz_stime' => $bank_sfz_stime,
                'reserved_mobile' => $reserved_mobile, //海科融通结算卡预留手机号
            ];

            $store = Store::where('store_id', $store_id)->first();
            $store_bank = StoreBank::where('store_id', $store_id)->first();
            $store_img = StoreImg::where('store_id', $store_id)->first();

            if ($store) {
                $is_store = 1;
            } else {
                $is_store = 0;
            }

            $is_merchant = 0;
            //判断手机号是否被注册
            if ($people_phone) {
                //验证手机号
                if (!preg_match("/^1[3456789]{1}\d{9}$/", $people_phone)) {
                    return json_encode([
                        'status' => 2,
                        'message' => '手机号码格式不正确'
                    ]);
                }

                $Merchant = Merchant::where('phone', $people_phone)
                    ->select('id')->first();

                if ($Merchant) {
                    $is_merchant = 1;
                }

                if ($is_merchant == 1 && $is_store == 0) {
                    return json_encode([
                        'status' => 2,
                        'message' => '手机号已被注册,请更换'
                    ]);
                }
            }

            //开启事务
            try {
                DB::beginTransaction();

                //入库账户
                if ($people_phone) {
                    //未注册
                    if ($is_merchant == 0) {
                        //注册账户
                        $dataIN = [
                            'pid' => 0,
                            'type' => 1,
                            'name' => $people,
                            'email' => '',
                            'password' => bcrypt('000000'),
                            'pay_password' => bcrypt('000000'),
                            'phone' => $people_phone,
                            'user_id' => $user_id, //推广员id
                            'config_id' => $config_id,
                            'wx_openid' => ''
                        ];
                        $merchant = Merchant::create($dataIN);
                        $merchant_id = $merchant->id;

                        $stores['merchant_id'] = $merchant_id;
                        MerchantStore::create([
                            'store_id' => $store_id,
                            'merchant_id' => $merchant_id
                        ]);

                    }
                }

                if ($store) {
                    //修改   不修改归属ID
                    $stores['user_id'] = $store->user_id;
                    $stores['config_id'] = $store->config_id;
                    $stores = array_filter($stores, function ($v) {
                        if ($v == "") {
                            return false;
                        } else {
                            return true;
                        }
                    });
                    $store->update($stores);
                    $store->save();
                } else {

                    $rules = [
                        'store_name' => 'unique:stores',
                    ];
                    $validator = Validator::make($data, $rules);
                    if ($validator->fails()) {
                        return json_encode([
                            'status' => 2,
                            'message' => '门店名称在系统中已经存在',
                        ]);
                    }

                    Store::create($stores);
                }
                if ($store_img) {
                    $store_img->update(array_filter($store_imgs));
                    $store_img->save();
                } else {
                    StoreImg::create($store_imgs);
                }


                if ($store_bank) {
                    $store_bank->update(array_filter($store_banks));
                    $store_bank->save();
                } else {
                    $stores['store_type'] = 1;
                    $stores['store_type_name'] = '个体';
                    StoreBank::create($store_banks);
                }

                DB::commit();
            } catch (\Exception $e) {
                Log::info($e);
                DB::rollBack();
                return json_encode(['status' => -1, 'message' => $e->getMessage()]);
            }

            return json_encode([
                'status' => 1,
                'message' => '资料添加成功',
                'data' => [
                    'store_id' => $store_id
                ]
            ]);
        } catch (\Exception $exception) {
            Log::info($exception);
            $this->status = -1;
            $this->message = $exception->getMessage();
            return $this->format();
        }
    }


    //安装人员获取门店信息
    public function storeListToInstall(Request $request)
    {
        try {
            $token = $this->parseToken();
            $install_uer = $request->post('installUser', ''); //安装业务员用户id
            $store_id = $request->post('storeId', ''); //门店id
            $province_code = $request->post('provinceCode'); //省份code
            $city_code = $request->post('cityCode'); //城市code
            $area_code = $request->post('areaCode'); //地区code
            $install_status = $request->post('installStatus', 0); //安装状态

            $where = [];
            $where[] = ['stores.is_delete', '=', 0];
            $where[] = ['stores.is_close', '=', 0];

            if ($install_uer) {
                $where[] = ['stores.installUser', '=', $install_uer];
            }

            if ($store_id) {
                $where[] = ['stores.store_id', '=', $store_id];
            }

            if ($province_code) {
                $where[] = ['stores.province_code', '=', $province_code];
            }

            if ($city_code) {
                $where[] = ['stores.city_code', '=', $city_code];
            }

            if ($area_code) {
                $where[] = ['stores.area_code', '=', $area_code];
            }

            $obj = Store::where($where)
                ->select('id', 'pid', 'store_id', 'installUser', 'merchant_id', 'store_name', 'store_short_name', 'people_phone', 'province_name', 'city_name', 'area_name', 'store_address', 'store_type', 'lat', 'lng', 'installStatus', 'created_at', 'updated_at')
                ->where('stores.installStatus', '=', $install_status)
                ->orderBy('updated_at', 'desc');

            $this->t = $obj->count();
            $data = $this->page($obj)->get();

            $this->status = 1;
            $this->message = '数据返回成功';
            return $this->format($data);
        } catch (\Exception $ex) {
            $this->status = -1;
            $this->message = $ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine();
            return $this->format();
        }

    }


    //更新安装进度
    public function updateStoreInstallStatus(Request $request)
    {
        try {
            $token = $this->parseToken();
            $install_uer = $request->post('installUser', ''); //安装业务员用户id
            $store_id = $request->post('storeId', ''); //门店id
            $install_status = $request->post('installStatus', 0); //安装状态

            $user_info = User::where('id', $install_uer)->first();
            if (!$user_info) {
                $this->status = 2;
                $this->message = '无此业务员信息';
                return $this->format();
            }

            $store_info = Store::where('store_id', $store_id)
                ->where('is_close', 0)
                ->where('is_delete', 0)
                ->first();
            if (!$store_info) {
                $this->status = 2;
                $this->message = '无此门店信息或状态异常';
                return $this->format();
            }

            if ($store_info->installStatus == $install_status) {
                $this->status = 2;
                $this->message = '请勿重复更新安装状态';
                return $this->format();
            }

            $updateData = [
                'installStatus' => $install_status
            ];
            if ($install_uer) $updateData['installUser'] = $install_uer;
            $obj = $store_info->update($updateData);
            if ($obj) {
                $this->status = 1;
                $this->message = '更新状态成功';
                return $this->format();
            } else {
                $this->status = 2;
                $this->message = '更新状态失败';
                return $this->format();
            }
        } catch (\Exception $ex) {
            $this->status = -1;
            $this->message = $ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine();
            return $this->format();
        }
    }


    //达达配置
    public function updateDaDaConfig(Request $request)
    {
        $token = $this->parseToken();
        $type = $request->post('type', '');
        $store_id = $request->post('storeId', '');
        $source_id = $request->post('sourceId', '');
        $shop_no = $request->post('shopNo', '');

        if ($type) {
            $dada_merchants = DadaMerchant::where('store_id', $store_id)
                ->select('store_id', 'source_id', 'status')
                ->first();
            $dada_orders = DadaStore::where('store_id', $store_id)
                ->select('store_id', 'shop_no', 'origin_shop_id', 'new_shop_id', 'status')
                ->first();

            $data = [];
            if ($dada_merchants) {
                $data['source_id'] = $dada_merchants->source_id;
            }
            if ($dada_orders) {
                $data['shop_no'] = $dada_orders->shop_no;
            }
            $this->status = 200;
            $this->message = '数据返回成功';
            return $this->format($data);
        }

        try {
            $merchants_res = DadaMerchant::where('store_id', $store_id)->first();
            $store_res = DadaStore::where('store_id', $store_id)->first();

            if ($source_id && $shop_no) {
                if ($merchants_res && $store_res) {
                    $merchants_up_res = $merchants_res->update([
                        'source_id' => $source_id
                    ]);

                    $store_up_res = $store_res->update([
                        'shop_no' => $shop_no
                    ]);

                    if ($merchants_up_res && $store_up_res) {
                        $this->status = 200;
                        $this->message = '更新成功';
                        return $this->format();
                    }
                } else {
                    $merchants_create_res = DadaMerchant::create([
                        'store_id' => $store_id,
                        'source_id' => $source_id,
                        'status' => 1
                    ]);

                    $store_create_res = DadaStore::create([
                        'store_id' => $store_id,
                        'shop_no' => $shop_no,
                        'status' => 1
                    ]);

                    if ($merchants_create_res && $store_create_res) {
                        $this->status = 200;
                        $this->message = '创建成功';
                        return $this->format();
                    }
                }
            }

            $this->status = 200;
            $this->message = '没有任何更改';
            return $this->format();
        } catch (\Exception $e) {
            $this->status = -1;
            $this->message = $e->getMessage().' | '.$e->getFile().' | '.$e->getLine();
            return $this->format();
        }
    }


    //微信实名认证 提交
    public function applyWeChatRealName(Request $request)
    {
        try {
            $token = $this->parseToken();
            $ways_type = $request->post('waysType', ''); //支付类型
            $store_id = $request->post('storeId', ''); //门店id

            $check_data = [
                'waysType' => '支付类型',
                'storeId' => '门店ID'
            ];
            $check = $this->check_required($request->except(['token']), $check_data);
            if ($check) {
                $this->status = 2;
                $this->message = $check;
                return $this->format();
            }

            //门店是否存在
            $store_obj = Store::where('store_id', $store_id)
                ->where('is_close', 0)
                ->where('is_delete', 0)
                ->first();
            if (!$store_obj) {
                $this->status = 2;
                $this->message = '门店不存在或状态异常';
                return $this->format();
            }
            $config_id = $store_obj->config_id;

            //通道是否开通
            $store_pay_ways_obj = StorePayWay::where('ways_type', $ways_type)
                ->where('store_id', $store_id)
                ->where('status', 1)
                ->first();
            if (!$store_pay_ways_obj) {
                $this->status = 2;
                $this->message = '通道未开通或状态异常';
                return $this->format();
            }

            //随行付
            if ( (12999 < $ways_type) && ($ways_type  < 13999) ) {
                $config_obj = new VbillConfigController();
                $vbill_config_obj = $config_obj->vbill_config($config_id);
                if (!$vbill_config_obj) {
                    $this->status = 2;
                    $this->message = '随行付支付未配置';
                    return $this->format();
                }

                $vbill_merchant_obj = $config_obj->vbill_merchant($store_id, 0);
                if (!$vbill_merchant_obj) {
                    $this->status = 2;
                    $this->message = '随行付-商户未入网';
                    return $this->format();
                }
                if (!$vbill_merchant_obj->mno) {
                    $this->status = 2;
                    $this->message = '随行付-商户号不存在';
                    return $this->format();
                }

                if ( ($vbill_merchant_obj->idenStatus == 0) || ($vbill_merchant_obj->idenStatus == 7) ) {
                    $vbill_pay_obj = new PayController();
                    $commit_apply_data = [
                        'mno' => $vbill_merchant_obj->mno,
//                    'backUrl' => url('/api/vbill/weChatApplyNotify'),
                        'orgId' => $vbill_config_obj->orgId,
                        'privateKey' => $vbill_config_obj->privateKey,
                        'sxfpublic' => $vbill_config_obj->sxfpublic
                    ];
                    $result = $vbill_pay_obj->weChatRealNameCommitApply($commit_apply_data);

                    if ($result['status'] == 1) {
                        $wxApplyNo = isset($result['data']['wxApplyNo']) ? $result['data']['wxApplyNo'] : '';
                        if (!$wxApplyNo) {
                            $this->status = 2;
                            $this->message = '随行付-微信申请单编号-未返回';
                            return $this->format();
                        }
                        $update_res = $vbill_merchant_obj->update([
                            'wxApplyNo' => $wxApplyNo,
                            'idenStatus' => 4
                        ]);
                        if ($update_res) {
                            $this->status = 1;
                            $this->message = '操作成功';
                            return $this->format($vbill_merchant_obj);
                        } else {
                            $this->status = 2;
                            $this->message = '随行付-微信申请单编号-入库失败';
                            return $this->format($vbill_merchant_obj);
                        }
                    } else {
                        $this->status = 2;
                        $this->message = $result['message'] ?? '随行付-微信实名认证-失败';
                        return $this->format($vbill_merchant_obj);
                    }
                } else {
                    $this->status = 1;
                    $this->message = "'未认证'或'已作废'状态方可提交认证";
                    return $this->format($vbill_merchant_obj);
                }
            }

            //随行付a
            if ( (18999 < $ways_type) && ($ways_type  < 19999) ) {
                $config_obj = new VbillConfigController();
                $vbill_config_obj = $config_obj->vbilla_config($config_id);
                if (!$vbill_config_obj) {
                    $this->status = 2;
                    $this->message = '随行付a-支付未配置';
                    return $this->format();
                }

                $vbill_merchant_obj = $config_obj->vbilla_merchant($store_id, 0);
                if (!$vbill_merchant_obj) {
                    $this->status = 2;
                    $this->message = '随行付a-商户尚未入网';
                    return $this->format();
                }
                if (!$vbill_merchant_obj->mno) {
                    $this->status = 2;
                    $this->message = '随行付a-商户号不存在';
                    return $this->format();
                }

                if ( ($vbill_merchant_obj->idenStatus == 0) || ($vbill_merchant_obj->idenStatus == 7) ) {
                    $vbill_pay_obj = new PayController();
                    $commit_apply_data = [
                        'mno' => $vbill_merchant_obj->mno,
//                    'backUrl' => url('/api/vbill/weChatApplyNotifyA'),
                        'orgId' => $vbill_config_obj->orgId,
                        'privateKey' => $vbill_config_obj->privateKey,
                        'sxfpublic' => $vbill_config_obj->sxfpublic
                    ];
                    $result = $vbill_pay_obj->weChatRealNameCommitApply($commit_apply_data);

                    if ($result['status'] == 1) {
                        $wxApplyNo = isset($result['data']['wxApplyNo']) ? $result['data']['wxApplyNo'] : '';
                        if (!$wxApplyNo) {
                            $this->status = 2;
                            $this->message = '随行付-微信申请单编号-未返回';
                            return $this->format();
                        }
                        $update_res = $vbill_merchant_obj->update([
                            'wxApplyNo' => $wxApplyNo,
                            'idenStatus' => 4
                        ]);
                        if ($update_res) {
                            $this->status = 1;
                            $this->message = '操作成功';
                            return $this->format($vbill_merchant_obj);
                        } else {
                            $this->status = 2;
                            $this->message = '随行付a-微信申请单编号-入库失败';
                            return $this->format($vbill_merchant_obj);
                        }
                    } else {
                        $this->status = 2;
                        $this->message = $result['message'] ?? '随行付a-微信实名认证-失败';
                        return $this->format($vbill_merchant_obj);
                    }
                } else {
                    $this->status = 1;
                    $this->message = "'未认证'或'已作废'状态方可提交认证";
                    return $this->format($vbill_merchant_obj);
                }
            }

            $this->status = 2;
            $this->message = '所选支付类型不存在';
            return $this->format();
        } catch (\Exception $ex) {
            Log::info('提交微信实名认证error:');
            Log::info($ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine());
            $this->status = -1;
            $this->message = $ex->getMessage().' | '.$ex->getLine();
            return $this->format();
        }
    }


    //微信实名认证 结果查询
    public function queryWeChatRealName(Request $request)
    {
        try {
            $token = $this->parseToken();
            $ways_type = $request->post('waysType', ''); //支付类型
            $store_id = $request->post('storeId', ''); //门店id

            $check_data = [
                'waysType' => '支付类型',
                'storeId' => '门店ID'
            ];
            $check = $this->check_required($request->except(['token']), $check_data);
            if ($check) {
                $this->status = 2;
                $this->message = $check;
                return $this->format();
            }

            //门店是否存在
            $store_obj = Store::where('store_id', $store_id)
                ->where('is_close', 0)
                ->where('is_delete', 0)
                ->first();
            if (!$store_obj) {
                $this->status = 2;
                $this->message = '门店不存在或状态异常';
                return $this->format();
            }
            $config_id = $store_obj->config_id;

            //通道是否开通
            $store_pay_ways_obj = StorePayWay::where('ways_type', $ways_type)
                ->where('store_id', $store_id)
                ->where('status', 1)
                ->first();
            if (!$store_pay_ways_obj) {
                $this->status = 2;
                $this->message = '通道未开通或状态异常';
                return $this->format();
            }

            //随行付
            if ( (12999 < $ways_type) && ($ways_type  < 13999) ) {
                $config_obj = new VbillConfigController();
                $vbill_config_obj = $config_obj->vbill_config($config_id);
                if (!$vbill_config_obj) {
                    $this->status = 2;
                    $this->message = '随行付支付未配置';
                    return $this->format();
                }
                $vbill_merchant_obj = $config_obj->vbill_merchant($store_id, 0);
                if (!$vbill_merchant_obj) {
                    $this->status = 2;
                    $this->message = '随行付-商户未入网';
                    return $this->format();
                }
                if (!$vbill_merchant_obj->mno) {
                    $this->status = 2;
                    $this->message = '随行付-商户号不存在';
                    return $this->format();
                }
                if (!$vbill_merchant_obj->wxApplyNo) {
                    $this->status = 2;
                    $this->message = '随行付-微信申请单编号不存在';
                    return $this->format();
                }

                $vbill_pay_obj = new PayController();
                $commit_apply_data = [
                    'mno' => $vbill_merchant_obj->mno,
                    'wxApplyNo' => $vbill_merchant_obj->wxApplyNo,
                    'orgId' => $vbill_config_obj->orgId,
                    'privateKey' => $vbill_config_obj->privateKey,
                    'sxfpublic' => $vbill_config_obj->sxfpublic
                ];
                $result = $vbill_pay_obj->weChatRealNameQuery($commit_apply_data);

                if ($result['status'] == 1) {
                    $idenStatus = isset($result['data']['idenStatus']) ? $result['data']['idenStatus'] : ''; //认证状态
                    $infoQrcode = isset($result['data']['infoQrcode']) ? $result['data']['infoQrcode'] : ''; //确认二维码链接地址
                    $rejectCode = isset($result['data']['rejectCode']) ? $result['data']['rejectCode'] : ''; //驳回参数
                    $rejectInfo = isset($result['data']['rejectInfo']) ? $result['data']['rejectInfo'] : ''; //驳回原因
                    if (!$idenStatus) {
                        $this->status = 2;
                        $this->message = '随行付-微信实名认证状态-未返回';
                        return $this->format();
                    }

                    switch ($idenStatus)
                    {
                        case 'APPLYMENT_STATE_WAITTING_FOR_AUDIT':   $iden_status = 4;
                            break;
                        case 'APPLYMENT_STATE_WAITTING_FOR_CONFIRM_CONTACT':   $iden_status = 2;
                            break;
                        case 'APPLYMENT_STATE_WAITTING_FOR_CONFIRM_LEGALPERSON':   $iden_status = 3;
                            break;
                        case 'APPLYMENT_STATE_PASSED':   $iden_status = 1;
                            break;
                        case 'APPLYMENT_STATE_REJECTED':   $iden_status = 5;
                            break;
                        case 'APPLYMENT_STATE_FREEZED':   $iden_status = 6;
                            break;
                        case 'APPLYMENT_STATE_CANCELED':   $iden_status = 7;
                            break;
                        case 'APPLYMENT_STATE_OPENACCOUNT':   $iden_status = 8;
                            break;
                        default:
                            $iden_status = 9;
                    }

                    if (!$iden_status) {
                        $this->status = 2;
                        $this->message = '随行付-微信实名认证状态-未知';
                        return $this->format();
                    }

                    $update_data = [
                        'idenStatus' => $iden_status, //0-未认证;1-审核通过;2-待确认联系信息;3-待账户验证;4-审核中;5-审核驳回;6-已冻结;7-已作废;8-重复认证;9-其他
                    ];
                    if($infoQrcode) $update_data['infoQrcode'] = $infoQrcode;
                    if($rejectCode) $update_data['rejectCode'] = $rejectCode;
                    if($rejectInfo) $update_data['rejectInfo'] = $rejectInfo;
                    $update_res = $vbill_merchant_obj->update($update_data);
                    if ($update_res) {
                        $this->status = 1;
                        $this->message = '操作成功';
                        return $this->format($vbill_merchant_obj);
                    } else {
                        $this->status = 2;
                        $this->message = '随行付-微信实名认证查询结果-更新失败';
                        return $this->format($vbill_merchant_obj);
                    }
                } else {
                    $this->status = 2;
                    $this->message = $result['message'] ?? '随行付-微信实名认证查询-失败';
                    return $this->format($vbill_merchant_obj);
                }
            }

            //随行付a
            if ( (18999 < $ways_type) && ($ways_type  < 19999) ) {
                $config_obj = new VbillConfigController();
                $vbill_config_obj = $config_obj->vbilla_config($config_id);
                if (!$vbill_config_obj) {
                    $this->status = 2;
                    $this->message = '随行付a-支付未配置';
                    return $this->format();
                }

                $vbill_merchant_obj = $config_obj->vbilla_merchant($store_id, 0);
                if (!$vbill_merchant_obj) {
                    $this->status = 2;
                    $this->message = '随行付a-商户尚未入网';
                    return $this->format();
                }
                if (!$vbill_merchant_obj->mno) {
                    $this->status = 2;
                    $this->message = '随行付a-商户号不存在';
                    return $this->format();
                }
                if (!$vbill_merchant_obj->wxApplyNo) {
                    $this->status = 2;
                    $this->message = '随行付a-微信申请单编号不存在';
                    return $this->format();
                }

                $vbill_pay_obj = new PayController();
                $commit_apply_data = [
                    'mno' => $vbill_merchant_obj->mno,
                    'wxApplyNo' => $vbill_merchant_obj->wxApplyNo,
                    'orgId' => $vbill_config_obj->orgId,
                    'privateKey' => $vbill_config_obj->privateKey,
                    'sxfpublic' => $vbill_config_obj->sxfpublic
                ];
                $result = $vbill_pay_obj->weChatRealNameQuery($commit_apply_data);

                if ($result['status'] == 1) {
                    $idenStatus = isset($result['data']['idenStatus']) ? $result['data']['idenStatus'] : ''; //认证状态
                    $infoQrcode = isset($result['data']['infoQrcode']) ? $result['data']['infoQrcode'] : ''; //确认二维码链接地址
                    $rejectCode = isset($result['data']['rejectCode']) ? $result['data']['rejectCode'] : ''; //驳回参数
                    $rejectInfo = isset($result['data']['rejectInfo']) ? $result['data']['rejectInfo'] : ''; //驳回原因
                    if (!$idenStatus) {
                        $this->status = 2;
                        $this->message = '随行付a-微信实名认证状态-未返回';
                        return $this->format();
                    }

                    switch ($idenStatus)
                    {
                        case 'APPLYMENT_STATE_WAITTING_FOR_AUDIT':   $iden_status = 4;
                            break;
                        case 'APPLYMENT_STATE_WAITTING_FOR_CONFIRM_CONTACT':   $iden_status = 2;
                            break;
                        case 'APPLYMENT_STATE_WAITTING_FOR_CONFIRM_LEGALPERSON':   $iden_status = 3;
                            break;
                        case 'APPLYMENT_STATE_PASSED':   $iden_status = 1;
                            break;
                        case 'APPLYMENT_STATE_REJECTED':   $iden_status = 5;
                            break;
                        case 'APPLYMENT_STATE_FREEZED':   $iden_status = 6;
                            break;
                        case 'APPLYMENT_STATE_CANCELED':   $iden_status = 7;
                            break;
                        case 'APPLYMENT_STATE_OPENACCOUNT':   $iden_status = 8;
                            break;
                        default:
                            $iden_status = 9;
                    }

                    if (!$iden_status) {
                        $this->status = 2;
                        $this->message = '随行付a-微信实名认证状态-未知';
                        return $this->format();
                    }

                    $update_data = [
                        'idenStatus' => $iden_status, //0-未认证;1-审核通过;2-待确认联系信息;3-待账户验证;4-审核中;5-审核驳回;6-已冻结;7-已作废;8-重复认证;9-其他
                    ];
                    if($infoQrcode) $update_data['infoQrcode'] = $infoQrcode;
                    if($rejectCode) $update_data['rejectCode'] = $rejectCode;
                    if($rejectInfo) $update_data['rejectInfo'] = $rejectInfo;
                    $update_res = $vbill_merchant_obj->update($update_data);
                    if ($update_res) {
                        $this->status = 1;
                        $this->message = '操作成功';
                        return $this->format($vbill_merchant_obj);
                    } else {
                        $this->status = 2;
                        $this->message = '随行付a-微信实名认证查询结果-更新失败';
                        return $this->format($vbill_merchant_obj);
                    }
                } else {
                    $this->status = 2;
                    $this->message = $result['message'] ?? '随行付a-微信实名认证查询-失败';
                    return $this->format($vbill_merchant_obj);
                }
            }

            $this->status = 2;
            $this->message = '所选支付类型不存在';
            return $this->format();
        } catch (\Exception $ex) {
            Log::info('查询-微信实名认证error:');
            Log::info($ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine());
            $this->status = -1;
            $this->message = $ex->getMessage().' | '.$ex->getLine();
            return $this->format();
        }
    }


    //微信实名认证 申请撤销
    public function backWeChatRealName(Request $request)
    {
        try {
            $token = $this->parseToken();
            $ways_type = $request->post('waysType', ''); //支付类型
            $store_id = $request->post('storeId', ''); //门店id

            $check_data = [
                'waysType' => '支付类型',
                'storeId' => '门店ID'
            ];
            $check = $this->check_required($request->except(['token']), $check_data);
            if ($check) {
                $this->status = 2;
                $this->message = $check;
                return $this->format();
            }

            //门店是否存在
            $store_obj = Store::where('store_id', $store_id)
                ->where('is_close', 0)
                ->where('is_delete', 0)
                ->first();
            if (!$store_obj) {
                $this->status = 2;
                $this->message = '门店不存在或状态异常';
                return $this->format();
            }
            $config_id = $store_obj->config_id;

            //通道是否开通
            $store_pay_ways_obj = StorePayWay::where('ways_type', $ways_type)
                ->where('store_id', $store_id)
                ->where('status', 1)
                ->first();
            if (!$store_pay_ways_obj) {
                $this->status = 2;
                $this->message = '通道未开通或状态异常';
                return $this->format();
            }

            //随行付
            if ( (12999 < $ways_type) && ($ways_type < 13999) ) {
                $config_obj = new VbillConfigController();
                $vbill_config_obj = $config_obj->vbill_config($config_id);
                if (!$vbill_config_obj) {
                    $this->status = 2;
                    $this->message = '随行付支付未配置';
                    return $this->format();
                }
                $vbill_merchant_obj = $config_obj->vbill_merchant($store_id, 0);
                if (!$vbill_merchant_obj) {
                    $this->status = 2;
                    $this->message = '随行付-商户未入网';
                    return $this->format();
                }
                if (!$vbill_merchant_obj->mno) {
                    $this->status = 2;
                    $this->message = '随行付-商户号不存在';
                    return $this->format();
                }
                if (!$vbill_merchant_obj->wxApplyNo) {
                    $this->status = 2;
                    $this->message = '随行付-微信申请单编号不存在';
                    return $this->format();
                }

                if ( ($vbill_merchant_obj->idenStatus == 4) || ($vbill_merchant_obj->idenStatus == 5) ) {
                    $vbill_pay_obj = new PayController();
                    $commit_apply_data = [
                        'mno' => $vbill_merchant_obj->mno,
                        'wxApplyNo' => $vbill_merchant_obj->wxApplyNo,
                        'orgId' => $vbill_config_obj->orgId,
                        'privateKey' => $vbill_config_obj->privateKey,
                        'sxfpublic' => $vbill_config_obj->sxfpublic
                    ];
                    $result = $vbill_pay_obj->weChatRealNameBack($commit_apply_data);
                    if ($result['status'] == 1) {
                        $update_data = [
                            'idenStatus' => 7, //0-未认证;1-审核通过;2-待确认联系信息;3-待账户验证;4-审核中;5-审核驳回;6-已冻结;7-已作废(表示申请单已被撤销,无需再对其进行操作);8-重复认证;9-其他
                        ];
                        $update_res = $vbill_merchant_obj->update($update_data);
                        if ($update_res) {
                            $this->status = 1;
                            $this->message = '撤销成功';
                            return $this->format($vbill_merchant_obj);
                        } else {
                            $this->status = 2;
                            $this->message = '随行付-微信实名认证撤销-更新库失败';
                            return $this->format($vbill_merchant_obj);
                        }
                    } else {
                        $this->status = 2;
                        $this->message = $result['message'] ?? '随行付-微信实名认证撤销-失败';
                        return $this->format($vbill_merchant_obj);
                    }
                } else {
                    $this->status = 2;
                    $this->message = "'审核中'或'审核驳回'方可撤销";
                    return $this->format($vbill_merchant_obj);
                }

            }

            //随行付a
            if ( (18999 < $ways_type) && ($ways_type < 19999) ) {
                $config_obj = new VbillConfigController();
                $vbill_config_obj = $config_obj->vbilla_config($config_id);
                if (!$vbill_config_obj) {
                    $this->status = 2;
                    $this->message = '随行付a-支付未配置';
                    return $this->format();
                }

                $vbill_merchant_obj = $config_obj->vbilla_merchant($store_id, 0);
                if (!$vbill_merchant_obj) {
                    $this->status = 2;
                    $this->message = '随行付a-商户尚未入网';
                    return $this->format();
                }
                if (!$vbill_merchant_obj->mno) {
                    $this->status = 2;
                    $this->message = '随行付a-商户号不存在';
                    return $this->format();
                }
                if (!$vbill_merchant_obj->wxApplyNo) {
                    $this->status = 2;
                    $this->message = '随行付a-微信申请单编号不存在';
                    return $this->format();
                }

                if ( ($vbill_merchant_obj->idenStatus == 4) || ($vbill_merchant_obj->idenStatus == 5) ) {
                    $vbill_pay_obj = new PayController();
                    $commit_apply_data = [
                        'mno' => $vbill_merchant_obj->mno,
                        'wxApplyNo' => $vbill_merchant_obj->wxApplyNo,
                        'orgId' => $vbill_config_obj->orgId,
                        'privateKey' => $vbill_config_obj->privateKey,
                        'sxfpublic' => $vbill_config_obj->sxfpublic
                    ];
                    $result = $vbill_pay_obj->weChatRealNameBack($commit_apply_data);
                    if ($result['status'] == 1) {
                        $update_data = [
                            'idenStatus' => 7, //7-已作废(表示申请单已被撤销,无需再对其进行操作)
                        ];
                        $update_res = $vbill_merchant_obj->update($update_data);
                        if ($update_res) {
                            $this->status = 1;
                            $this->message = '撤销成功';
                            return $this->format($vbill_merchant_obj);
                        } else {
                            $this->status = 2;
                            $this->message = '随行付a-微信实名认证撤销-更新库失败';
                            return $this->format($vbill_merchant_obj);
                        }
                    } else {
                        $this->status = 2;
                        $this->message = $result['message'] ?? '随行付a-微信实名认证撤销-失败';
                        return $this->format($vbill_merchant_obj);
                    }
                } else {
                    $this->status = 2;
                    $this->message = "'审核中'或'审核驳回'方可撤销";
                    return $this->format($vbill_merchant_obj);
                }
            }

            $this->status = 2;
            $this->message = '所选支付类型不存在';
            return $this->format();
        } catch (\Exception $ex) {
            Log::info('微信实名认证-撤销-error:');
            Log::info($ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine());
            $this->status = -1;
            $this->message = $ex->getMessage().' | '.$ex->getLine();
            return $this->format();
        }
    }


    //微信子商户授权状态 查询
    public function queryGrantStatusWeChatRealName(Request $request)
    {
        try {
            $token = $this->parseToken();
            $ways_type = $request->post('waysType', ''); //支付类型
            $store_id = $request->post('storeId', ''); //门店id

            $check_data = [
                'waysType' => '支付类型',
                'storeId' => '门店ID'
            ];
            $check = $this->check_required($request->except(['token']), $check_data);
            if ($check) {
                $this->status = 2;
                $this->message = $check;
                return $this->format();
            }

            //门店是否存在
            $store_obj = Store::where('store_id', $store_id)
                ->where('is_close', 0)
                ->where('is_delete', 0)
                ->first();
            if (!$store_obj) {
                $this->status = 2;
                $this->message = '门店不存在或状态异常';
                return $this->format();
            }
            $config_id = $store_obj->config_id;

            //通道是否开通
            $store_pay_ways_obj = StorePayWay::where('ways_type', $ways_type)
                ->where('store_id', $store_id)
                ->where('status', 1)
                ->first();
            if (!$store_pay_ways_obj) {
                $this->status = 2;
                $this->message = '通道未开通或状态异常';
                return $this->format();
            }

            //随行付
            if ( (12999 < $ways_type) && ($ways_type  < 13999) ) {
                $config_obj = new VbillConfigController();
                $vbill_config_obj = $config_obj->vbill_config($config_id);
                if (!$vbill_config_obj) {
                    $this->status = 2;
                    $this->message = '随行付支付未配置';
                    return $this->format();
                }
                $vbill_merchant_obj = $config_obj->vbill_merchant($store_id, 0);
                if (!$vbill_merchant_obj) {
                    $this->status = 2;
                    $this->message = '随行付-商户未入网';
                    return $this->format();
                }
                if (!$vbill_merchant_obj->mno) {
                    $this->status = 2;
                    $this->message = '随行付-商户号不存在';
                    return $this->format();
                }
                if (!$vbill_merchant_obj->childNo) {
                    $this->status = 2;
                    $this->message = '随行付-微信子商户号不存在';
                    return $this->format();
                }

                $vbill_pay_obj = new PayController();
                $commit_apply_data = [
                    'childNo' => $vbill_merchant_obj->childNo,
                    'orgId' => $vbill_config_obj->orgId,
                    'privateKey' => $vbill_config_obj->privateKey,
                    'sxfpublic' => $vbill_config_obj->sxfpublic
                ];
                $result = $vbill_pay_obj->weChatRealNameQueryGrantStatus($commit_apply_data);

                if ($result['status'] == 1) {
                    $authStatus = isset($result['data']['authStatus']) ? $result['data']['authStatus'] : ''; //授权状态
                    if (!$authStatus) {
                        $this->status = 2;
                        $this->message = '随行付-微信子商户授权状态-未返回';
                        return $this->format();
                    }

                    if ($authStatus == 'AUTHORIZE_STATE_AUTHORIZED') {
                        $auth_status = 1;
                    }
                    elseif ($authStatus == 'AUTHORIZE_STATE_UNAUTHORIZED') {
                        $auth_status = 2;
                    }
                    else {
                        $this->status = 2;
                        $this->message = '随行付-微信子商户授权状态-未知';
                        return $this->format();
                    }

                    $update_data = [
                        'authStatus' => $auth_status, //0-未知;1-已授权;2--未授权
                    ];
                    $update_res = $vbill_merchant_obj->update($update_data);
                    if ($update_res) {
                        $this->status = 1;
                        $this->message = '成功';
                        return $this->format($vbill_merchant_obj);
                    } else {
                        $this->status = 2;
                        $this->message = '随行付-微信子商户授权状态-更新入库失败';
                        return $this->format($vbill_merchant_obj);
                    }

                } else {
                    $this->status = 2;
                    $this->message = $result['message'] ?? '随行付-微信子商户授权状态-查询失败';
                    return $this->format($vbill_merchant_obj);
                }
            }

            //随行付a
            if ( (18999 < $ways_type) && ($ways_type  < 19999) ) {
                $config_obj = new VbillConfigController();
                $vbill_config_obj = $config_obj->vbilla_config($config_id);
                if (!$vbill_config_obj) {
                    $this->status = 2;
                    $this->message = '随行付a-支付未配置';
                    return $this->format();
                }
                $vbill_merchant_obj = $config_obj->vbilla_merchant($store_id, 0);
                if (!$vbill_merchant_obj) {
                    $this->status = 2;
                    $this->message = '随行付a-商户未入网';
                    return $this->format();
                }
                if (!$vbill_merchant_obj->mno) {
                    $this->status = 2;
                    $this->message = '随行付a-商户号不存在';
                    return $this->format();
                }
                if (!$vbill_merchant_obj->childNo) {
                    $this->status = 2;
                    $this->message = '随行付a-微信子商户号不存在';
                    return $this->format();
                }

                $vbill_pay_obj = new PayController();
                $commit_apply_data = [
                    'childNo' => $vbill_merchant_obj->childNo,
                    'orgId' => $vbill_config_obj->orgId,
                    'privateKey' => $vbill_config_obj->privateKey,
                    'sxfpublic' => $vbill_config_obj->sxfpublic
                ];
                $result = $vbill_pay_obj->weChatRealNameQueryGrantStatus($commit_apply_data);

                if ($result['status'] == 1) {
                    $authStatus = isset($result['data']['authStatus']) ? $result['data']['authStatus'] : ''; //授权状态
                    if (!$authStatus) {
                        $this->status = 2;
                        $this->message = '随行付-微信子商户授权状态-未返回';
                        return $this->format();
                    }

                    if ($authStatus == 'AUTHORIZE_STATE_AUTHORIZED') {
                        $auth_status = 1;
                    }
                    elseif ($authStatus == 'AUTHORIZE_STATE_UNAUTHORIZED') {
                        $auth_status = 2;
                    }
                    else {
                        $this->status = 2;
                        $this->message = '随行付-微信子商户授权状态-未知';
                        return $this->format();
                    }

                    $update_data = [
                        'authStatus' => $auth_status, //0-未知;1-已授权;2--未授权
                    ];
                    $update_res = $vbill_merchant_obj->update($update_data);
                    if ($update_res) {
                        $this->status = 1;
                        $this->message = '成功';
                        return $this->format($vbill_merchant_obj);
                    } else {
                        $this->status = 2;
                        $this->message = '随行付a-微信子商户授权状态-更新入库失败';
                        return $this->format($vbill_merchant_obj);
                    }

                } else {
                    $this->status = 2;
                    $this->message = $result['message'] ?? '随行付a-微信子商户授权状态-查询失败';
                    return $this->format($vbill_merchant_obj);
                }
            }

            $this->status = 2;
            $this->message = '所选支付类型不存在';
            return $this->format();
        } catch (\Exception $ex) {
            Log::info('查询-微信子商户授权状态查询error:');
            Log::info($ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine());
            $this->status = -1;
            $this->message = $ex->getMessage().' | '.$ex->getLine();
            return $this->format();
        }
    }


    public function putVoice(Request $request)
    {
        try {
            $user = $this->parseToken();
            $config_id = $request->post('configId', '1234');
            $action_type = $request->post('type', 1); //1-读 0-减

            $list_obj = VoiceStatistic::where('config_id', $config_id)
                ->first();
            if (!$list_obj) {
                $this->status = 2;
                $this->message = '用户尚未购买套餐';
                return $this->format();
            }

            if ($action_type) {
                $this->status = 1;
                $this->message = '数据返回成功';
                return $this->format($list_obj);
            } else {
                if ($list_obj->avali_num < 1) {
                    $this->status = 2;
                    $this->message = '套餐可以次数已用完，请充值';
                    return $this->format();
                }

                $avali_num = $list_obj->avali_num - 1;
                $update_res = $list_obj->update([
                    'avali_num' => max($avali_num, 0)
                ]);
                if ($update_res) {
                    $this->status = 1;
                    $this->message = '更新可用次数成功';
                    return $this->format($list_obj);
                } else {
                    $this->status = 2;
                    $this->message = '更新可用次数失败';
                    return $this->format($list_obj);
                }
            }
        } catch (\Exception $ex) {
            Log::info('xunfei语音合成-error');
            Log::info($ex->getMessage() .' | '. $ex->getFile() .' | '. $ex->getLine());
            $this->status = -1;
            $this->message = $ex->getMessage() .' | '. $ex->getLine();
            return $this->format();
        }
    }


    public function voicePayQr(Request $request)
    {
        try {
            $user = $this->parseToken();

            $data = [
                'store_pay_qr' => 'https://test.yunsoyi.cn/qr?store_id=2020518155323231527&voiceType=1'
            ];

            $this->status = 1;
            $this->message = '数据返回成功';
            return $this->format($data);
        } catch (\Exception $ex) {
            $this->status = -1;
            $this->message = $ex->getMessage() .' | '. $ex->getLine();
            return $this->format();
        }
    }

    public function sign($array,$private_key){
        ksort($array);
        $bufSignSrc =$this->ToUrlParams($array);
        $private_key = chunk_split($private_key , 64, "\n");
        $key = "-----BEGIN RSA PRIVATE KEY-----\n".wordwrap($private_key)."-----END RSA PRIVATE KEY-----";
        openssl_sign($bufSignSrc, $signature, $key );

        $sign = base64_encode($signature);
        return $sign;
    }

    public function ToUrlParams(array $array){
        $buff = "";
        foreach ($array as $k => $v)
        {
            if($v != "" && !is_array($v)){
                $buff .= $k . "=" . $v . "&";
            }
        }

        $buff = trim($buff, "&");
        return $buff;
    }

    //发送请求操作仅供参考,不为最佳实践
    public function request($url,$params){
        $ch = curl_init();
        $this_header = array("content-type: application/x-www-form-urlencoded;charset=UTF-8");
        curl_setopt($ch,CURLOPT_HTTPHEADER,$this_header);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);

        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//如果不加验证,就设false,商户自行处理
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

        $output = curl_exec($ch);
        curl_close($ch);
        return  $output;
    }

    //邮储签名
    public function get_sign($data){
        $buff = "";
        $array = [
            'txnCode'   =>  isset($data['txnCode']) ? $data['txnCode'] : '',
            'platformID'   =>  isset($data['platformID']) ? $data['platformID'] : '',
            'reqTraceID'   =>  isset($data['reqTraceID']) ? $data['reqTraceID'] : '',
            'reqDate'   =>  isset($data['reqDate']) ? $data['reqDate'] : '',
            'txnAMT'   =>  isset($data['txnAMT']) ? $data['txnAMT'] : '',
            'mchtNo'   =>  isset($data['mchtNo']) ? $data['mchtNo'] : '',
            'respCd'   =>  isset($data['respCd']) ? $data['respCd'] : '',
        ];
        foreach ($array as $k => $v)
        {
            if($v != "" && !is_array($v)){
                $buff .=  $v . " ";
            }
        }
        $buff = trim($buff);
        return $buff;
    }

    public function http_curl1($url, $post_data)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1); // post数据
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不验证证书
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); // post的变量
        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($post_data)
            )
        );
        $output = curl_exec($ch);
        curl_close($ch);

        //返回数据
        return $output;
    }


}
